C# MVVM WPF列表框条目未更新
我最近开始学习MVVM模式,并创建了一个简单的应用程序来测试一些东西 我有一个简单的观点:C# MVVM WPF列表框条目未更新,c#,wpf,mvvm,listbox,observablecollection,C#,Wpf,Mvvm,Listbox,Observablecollection,我最近开始学习MVVM模式,并创建了一个简单的应用程序来测试一些东西 我有一个简单的观点: 包含可观察到的项目集合的列表框 删除按钮 新按钮 用于项目描述的文本框 项目值的文本框 除了更新项目描述时列表框条目没有更新之外,其他一切都正常。我读了一些关于这方面的文章,所以我认为这与CollectionChanged无关。我尝试了一些解决这个问题的可能办法,但没有一个奏效。所以,也许我的方法总有问题 希望有人能帮我解决这个问题 型号/Item.cs internal class Item : I
- 包含可观察到的项目集合的列表框
- 删除按钮
- 新按钮
- 用于项目描述的文本框
- 项目值的文本框
internal class Item : INotifyPropertyChanged
{
#region Fields
private string value;
private string description;
#endregion
#region Constructors
public Item()
{
}
public Item(string value, string description) {
this.description = description;
this.value = value;
}
#endregion
public String Value
{
get
{
return value;
}
set
{
this.value = value;
OnPropertyChanged("Value");
}
}
public String Description
{
get
{
return description;
}
set
{
description = value;
OnPropertyChanged("Description");
}
}
#region Overrides
public override string ToString()
{
return description;
}
#endregion String Override
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
ViewModel/MainViewModel.cs
...
private ObservableCollection<Item> items;
private Item selectedItem;
public ObservableCollection<Item> Items {
get
{
return items;
}
set
{
items = value;
OnPropertyChanged("Items");
}
}
public Item SelectedItem {
get
{
return selectedItem;
}
set
{
selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
...
。。。
私人可观测收集项目;
私有项目selectedItem;
公共可观测收集项目{
得到
{
退货项目;
}
设置
{
项目=价值;
不动产变更(“项目”);
}
}
公共项目选择项{
得到
{
返回selectedItem;
}
设置
{
选择editem=值;
OnPropertyChanged(“SelectedItem”);
}
}
...
View/MainWindow.xaml
...
<Button Content="New" Command="{Binding NewCommand}" />
<Button Content="Delete" Command="{Binding DeleteCommand}" />
<ListBox x:Name="lbxItems" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" />
<TextBox Text="{Binding SelectedItem.Description}" />
<TextBox Text="{Binding SelectedItem.Value}" />
...
。。。
...
如何在模型类中生成PropertyChangedEvent
试试这个:
internal class Range : INotifyPropertyChanged
{
private string _value;
public string Value
{
get { return _value; }
set
{
if (_value == value) return;
_value = value;
OnPropertyChanged("Value");
}
}
//Do same for the Description property
//Do not forgot implement INotifyPropertyChanged interface here
}
我希望这对你有帮助
哦!!在你的更新之后,它是清晰的。对于listbox项,您不使用任何datatemplate,因此WPF为不带DT的show项调用ToString()方法。但当您更新任何属性时,WPF都不知道这一点,因为对象并没有改变。
使用Datatemplate或尝试使用空字符串调用PropertyChanged。使用此ItemTemplate应该可以工作
<ListBox Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Value}" Margin="0,0,10,0/>
<TextBlock Text="{Binding Path=Description}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
对于列表框/文本框绑定,可以同时使用SelectedRange
和SelectedItem
。这是有意的吗?能否提供Item类的实现?当Description属性中的值被更新时,是否调用OnPropertyChanged(“Description”)?Hi-Dirk。我试图简化我的代码,把它发布在这里。更新了我的问题。这都是我的选择。谢谢你的提示!根据请求添加了Item类的完整实现。感谢您的提示,但我已经实现了INotifyPropertyChanged接口。我更新了我的问题并提供了Item类的完整实现。