简介:如何从UI更新数据模型中的Isdone属性? (我在Google上阅读的书都无济于事。我希望有一个简单的答案。)我正在尝试尽可能完整,仅显示相关代码。初始化后,ListView的复选框正确显示为被数据库Actiontaken的值选中。但是,当从UI中选中“复选框”时,Isdone属性的设置代码未命中。在Visual Studio 2010中,我将解决方案分为以下项目:1. Chaos.Data,2. Chaos.DataService,3. TestClient。Chaos.Data使用带有部分类的EntityFramework从数据库提供数据。在此,我为以下单独的业务规则添加了单独的文件:namespace Chaos.Data{ partial class Lab { private Boolean? isdone; public virtual Boolean? Isdone { get { isdone = (Actiontaken == 1 || Actiontaken == 129) ? true : false; return isdone; } set { if (this.isdone != value) { this.isdone = value; this.Actiontaken = 1; this.OnPropertyChanged("Isdone"); if (this.isdone == true) this.setallcheckbox("Isdone"); } } }Chaos.DataService是WCF服务。在此,我定义了:namespace Chaos.DataService{ [OperationContract] public IEnumerable<Lab> GetAllLabs(String groupid, DateTime? encountertime) { if (encountertime == null) return null; using (var context = new ChaosModel()) { var query = from lab in context.Labs where lab.Groupid == groupid && lab.Tposted.Date <= ((DateTime)encountertime).Date select lab; var result = context.CreateDetachedCopy(query.ToList()); return result; } }}TestClient使用对Chaos.DataService的引用(...是的,为了安全起见,我已经在TestClient中更新了服务引用。)TestClient的 View 如下:XAML<ListView ItemContainerStyle="{StaticResource ItemContStyle}" ItemsSource="{Binding Labs}" HorizontalAlignment="Stretch" Margin="12,99,0,103" Name="listViewLabs" VerticalAlignment="Stretch" > <ListView.View> <GridView> <GridViewColumn Header="Done" Width="40"> <GridViewColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding Isdone, Mode=TwoWay}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> .................C#代码隐藏: namespace Chaos.UI.View { public partial class PatientLabsWindow : Window { public PatientLabsWindow(EncounterDetail encounter) { InitializeComponent(); ViewModelPatientLabs vm = new ViewModelPatientLabs(encounter); this.DataContext = vm; vm.CloseAction = new Action(() => this.Close()); } }}在ViewModel中,我定义了: namespace Chaos.UI.ViewModel { class ViewModelPatientLabs : ViewModelBase { private ChaosServiceClient serviceClient = new ChaosServiceClient(); // Constructor public ViewModelPatientLabs(EncounterDetail encounter) { this.Encounter = encounter; } private EncounterDetail encounter; public EncounterDetail Encounter { get { return this.encounter; } set { this.encounter = value; this.OnPropertyChanged("Encounter"); this.GetAllLabs(); } } private void GetAllLabs() { // consume the WCF service. this.serviceClient.GetAllLabsCompleted += (s, e) => { this.Labs = e.Result; }; // call the WCF service -- Async this.serviceClient.GetAllLabsAsync(Encounter.groupid, Encounter.tencounter); } private IEnumerable<Lab> labs; public IEnumerable<Lab> Labs { get { return this.labs; } set { this.labs = value; this.OnPropertyChanged("Labs"); } }}}如果我错过了什么,请告诉我。再次重申一下,已从数据库模型正确更新了ListView的复选框。但是,从用户界面中选中该框不会在添加的Labs业务类中找到Isdone的设置代码。我怎样才能解决这个问题?有任何想法吗?编辑:Lab是Chaos.Data命名空间中的已定义类。它由Telerik DataAccess生成为 namespace Chaos.Data { public partial class Lab : INotifyPropertyChanged { ...... } }为了避免碰到这个自动生成的文件,我将自己的添加项和一个单独的文件添加到了部分类中,如上所示。编辑:我对这一切都很陌生。我发现有趣的是,在客户端项目中更新服务时,WCF将我的部分类添加到Lab实体中,如下所示: namespace Chaos.UI.ChaosService { using System.Runtime.Serialization; using System; ..............................[System.Runtime.Serialization.OptionalFieldAttribute()] private System.Nullable<bool> IsdoneField;[System.SerializableAttribute()]public partial class Lab : object, System.Runtime.Serialization.IExtensibleDataObject,System.ComponentModel.INotifyPropertyChanged {................... [System.Runtime.Serialization.OptionalFieldAttribute()] private System.Nullable<bool> IsdoneField;并且从WCF查询返回的Labs的类型为IEnumerable ,不是可观察的集合。那么View绑定(bind)是否正在更新WCF中的字段并且永不传输回模型的问题呢?请帮助某人?? 最佳答案 如果要使IsDone的setter属性在单击时被触发,则需要使Lab实现Istone的setter属性上的INotifyPropertyChanged并引发PropertyChanged事件。这是一个工作示例。看法<Window x:Class="WpfUserControl.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" DataContext="{Binding RelativeSource={RelativeSource Self}}"> <Grid> <ListView ItemsSource="{Binding Labs}" > <ListView.View> <GridView> <GridViewColumn Header="Done" Width="40"> <GridViewColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding IsDone, Mode=TwoWay}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> </Grid></Window>背后的代码public partial class MainWindow : Window{ public MainWindow() { InitializeComponent(); DataContext = new MainWindowViewModel(); }}public class MainWindowViewModel{ public MainWindowViewModel() { Labs = new[] { new Lab { IsDone = true } }; } public IEnumerable<Lab> Labs { get; set; }}public class Lab : INotifyPropertyChanged{ private bool? _isDone; public bool? IsDone { get { return _isDone; } set { _isDone = value; OnPropertyChanged("IsDone"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } }}关于wpf - 复选框IsChecked与EntityFramework的绑定(bind)在MVVM模式中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24816291/ 10-11 09:11