【问题标题】:MVVM Binding Observable Collection to view?MVVM Binding Observable Collection 来查看?
【发布时间】:2015-01-26 06:15:20
【问题描述】:

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

观点:

<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 ConditionsModel
{
    public int profileId { get; set; }
    public string condition { get; set; }
    public bool hasCondition { get; set; }
}

视图模型:

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);
    }
}

这是一个正确的实现吗?当用户选中或取消选中复选框时,我需要更新模型。但是当复选框被选中或未选中时,它不会引发属性更改事件。我是否也应该在模型上实现 INotifyPropertyChanged 接口?

我见过很多例子,但他们都有不同的方法,我很困惑。请显示这个的正确实现?

谢谢

【问题讨论】:

  • 它没有引发属性更改事件是什么意思?您希望它引发哪些房地产事件?更改为 hasCondition 不会引发事件

标签: c# wpf xaml mvvm


【解决方案1】:

我认为您错过了 DataTemplate 中的 DataType 属性。只需参考this

<DataTemplate DataType="{x:Type sampleApp:ConditionsModel}">

sampleApp 在标签内创建的命名空间引用中。 ConditionsModel 是您的模型类。

【讨论】:

  • @sony ,这足以解决问题吗?或者您还有其他问题吗?
【解决方案2】:

您需要为类ConditionsModel 实现INotifyPropertyChanged 并为要观察/同步的属性提高PropertyChangedEvent,因为它也是ViewModel。

对于ConditionsViewModel类,它是整个ListView的ViewModel,对于ConditionsModel,它是每一行的ViewModel。 ViewModel 可以叠加。如果ConditionsModel 是域模型,我的建议是添加一个新的ItemViewModel,因为它们属于不同的层。正确区分不同的层总是更好。

【讨论】:

    猜你喜欢
    • 2014-10-24
    • 2018-06-15
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 2018-11-09
    相关资源
    最近更新 更多