gpt4 book ai didi

mvvm - RaisePropertyChanged不适用于ObservableCollection

转载 作者:行者123 更新时间:2023-12-03 10:29:02 25 4
gpt4 key购买 nike

使用MVVM Light Toolkit更新UI时,我遇到了一个非常奇怪的问题。 RaisePropertyChanged对于我的ObservableCollection根本不起作用。

这是XAML代码:

<ListBox x:Name="list"  ItemsSource="{Binding ModelList}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"></TextBlock>
<CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<interaction:Interaction.Triggers>
<interaction:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding TempCommand}" CommandParameter="{Binding ElementName=list, Path=SelectedItem}"
PassEventArgsToCommand="True"/>
</interaction:EventTrigger>
</interaction:Interaction.Triggers>
</ListBox>

并且有ViewModel代码部分:
private Model _selectedItem;
public Model SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
RaisePropertyChanged("SelectedItem");
}
}

private ObservableCollection<Model> _modelList;
public ObservableCollection<Model> ModelList
{
get { return _modelList; }
set
{
_modelList = value;
RaisePropertyChanged("ModelList");
}
}

public RelayCommand<Model> TempCommand { get; private set; }
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
modelList = new ObservableCollection<Model>()
{
new Model()
{
IsChecked = true,
Name = "Temp1"
},
new Model()
{
IsChecked = false,
Name = "Temp2"
},
new Model()
{
IsChecked = false,
Name = "Temp3"
}
};
ModelList = modelList;

TempCommand = new RelayCommand<Model>(Model_SelectedItem);
}

private void Model_SelectedItem(Model item)
{
// What should I do here?
}

当我更改ModelList时-ListBox UI没有 react 。

有人可以帮助我吗?

最佳答案

问题解决了。

当您在ObservableCollection或List中具有自定义类时,它必须从ObservableObject派生,并且所有属性都必须引发RaisePropertyChanged事件。

关于mvvm - RaisePropertyChanged不适用于ObservableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16844040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com