C# 要查看的MVVM绑定可观察集合?

C# 要查看的MVVM绑定可观察集合?,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,在我的应用程序中,我需要将复选框列表绑定到可观察的集合。我看到了很多例子,但我找不到一个合适的实现,这就是为什么我张贴这个问题 观点: <Grid Name="GrdMain" Background="White"> <ListView Name="lstConditions" VerticalAlignment="Top" Height="150" ItemsSource="{Binding ConditionsModels}

在我的应用程序中,我需要将复选框列表绑定到可观察的集合。我看到了很多例子,但我找不到一个合适的实现,这就是为什么我张贴这个问题

观点:

<Grid Name="GrdMain" Background="White">    
    <ListView Name="lstConditions" VerticalAlignment="Top" Height="150" 
                ItemsSource="{Binding ConditionsModels}" Margin="0,25,0,0" BorderBrush="Transparent" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <CheckBox Content="{Binding Path=condition}" Margin="8" Style="{StaticResource CheckBoxDefault}"
                                IsChecked="{Binding hasCondition,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
                </StackPanel>                    
            </DataTemplate>
        </ListBox.ItemTemplate>    
    </ListView>
</grid>     
视图模型:

public class ConditionsViewModel : INotifyPropertyChanged
{

    private ConditionsModel _conditionsModel;
    private ObservableCollection<ConditionsModel> _conditionsModels;

    public ConditionsModel ConditionsModel
    {
        get
        {
            return _conditionsModel;
        }
        set
        {
            _conditionsModel = value;
            RaisePropertyChanged("ConditionsModel");
        }
    }


    public ObservableCollection<ConditionsModel> ConditionsModels
    {
        get
        {
            return _conditionsModels;
        }
        set
        {
            _conditionsModels = value;
            RaisePropertyChanged("ConditionsModels");
        }
    }


    public ConditionsViewModel(int profileId)
    {
        ConditionsModel = new ConditionsModel();
        ConditionsModels = new ObservableCollection<ConditionsModel>();
        ConditionsModels.CollectionChanged += ConditionsModels_CollectionChanged;
        GetConditions(profileId);
    }

    void ConditionsModels_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        RaisePropertyChanged("ConditionsModels");
    }


    private void GetConditions(int profileId)
    {
        HealthAssessmentRepository _rep = new HealthAssessmentRepository();
        _conditionsModels = _rep.GetConditions(profileId);
    }
}
公共类条件viewmodel:INotifyPropertyChanged
{
私有条件模型;
私人可观测采集模型;
公共条件模型条件模型
{
得到
{
返回条件模型;
}
设置
{
_条件模型=值;
RaisePropertyChanged(“条件模型”);
}
}
公共可观测收集条件模型
{
得到
{
返回条件模型;
}
设置
{
_条件模型=值;
RaisePropertyChanged(“条件模型”);
}
}
公共条件viewmodel(int profileId)
{
ConditionsModel=新ConditionsModel();
ConditionsModels=新的ObservableCollection();
ConditionsModels.CollectionChanged+=ConditionsModels\u CollectionChanged;
GetConditions(profileId);
}
void ConditionsModels\u CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
RaisePropertyChanged(“条件模型”);
}
私有void GetConditions(int profileId)
{
HealthAssessmentRepository _rep=新建HealthAssessmentRepository();
_conditionsModels=_rep.GetConditions(profileId);
}
}
这是正确的实现吗?我需要在用户选中或取消选中复选框时更新模型。但当复选框被选中或取消选中时,它不会引发Property changed事件。我是否也应该在模型上实现INotifyPropertyChanged接口

我见过很多例子,但它们都有不同的方法,我感到困惑。请说明此操作的正确实现方式


谢谢

您需要为类
条件模型
实现
INotifyPropertyChanged
,并为要观察/同步的属性提升
PropertyChangedEvent
,因为它也是ViewModel


对于类
条件视图模型
,它是整个
列表视图
的视图模型;对于
条件模型
,它是每行的视图模型。ViewModel可以覆盖。如果
ConditionsModel
是域模型,我的建议是添加一个新的ItemViewModel,因为它们属于不同的层。正确区分不同的层总是更好的。

我认为您错过了DataTemplate中的数据类型属性。仅参考



此处sampleApp位于标记内创建的命名空间引用中。而条件模型是您的模型类。

您认为它不引发属性更改事件是什么意思?你认为它会引发哪场房地产事件?更改为hasCondition将不会引发event@sony,这是否足以解决问题?或者你面临其他问题?
public class ConditionsViewModel : INotifyPropertyChanged
{

    private ConditionsModel _conditionsModel;
    private ObservableCollection<ConditionsModel> _conditionsModels;

    public ConditionsModel ConditionsModel
    {
        get
        {
            return _conditionsModel;
        }
        set
        {
            _conditionsModel = value;
            RaisePropertyChanged("ConditionsModel");
        }
    }


    public ObservableCollection<ConditionsModel> ConditionsModels
    {
        get
        {
            return _conditionsModels;
        }
        set
        {
            _conditionsModels = value;
            RaisePropertyChanged("ConditionsModels");
        }
    }


    public ConditionsViewModel(int profileId)
    {
        ConditionsModel = new ConditionsModel();
        ConditionsModels = new ObservableCollection<ConditionsModel>();
        ConditionsModels.CollectionChanged += ConditionsModels_CollectionChanged;
        GetConditions(profileId);
    }

    void ConditionsModels_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        RaisePropertyChanged("ConditionsModels");
    }


    private void GetConditions(int profileId)
    {
        HealthAssessmentRepository _rep = new HealthAssessmentRepository();
        _conditionsModels = _rep.GetConditions(profileId);
    }
}
<DataTemplate DataType="{x:Type sampleApp:ConditionsModel}">