【发布时间】:2015-09-12 00:33:30
【问题描述】:
我有一个类似How to get an ItemsSource to refresh its bind?的问题
但我确实使用了 INotifyPropertyChange 接口,但问题仍然存在。这是 XAML:
<UserControl x:Class="Sample.Module.Pages.View.ModifyDataTypeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
xmlns:local="clr-namespace:Sample.Module.Pages.ViewModel"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="350">
<UserControl.DataContext>
<local:DataTypeViewModel/>
</UserControl.DataContext>
<Canvas>
<ComboBox Canvas.Left="0" Canvas.Top="0"
Margin="13,22,0,0"
Style="{DynamicResource VirtualisedMetroComboBox}"
Controls:TextBoxHelper.Watermark="Autocompletion"
DisplayMemberPath="DataTypeName"
IsEditable="True"
ItemsSource="{Binding DataTypes}"
MaxDropDownHeight="125"
SelectedItem="{Binding Path=SelectedDataType, Mode=TwoWay}" Height="25"/>
</Canvas>
这是视图模型:
public class DataTypeViewModel : DomainObject
{
private ObservableCollection<DataTypeRepository> dataTypes;
private DataTypeRepository selectedDataType;
private DataTypeModel dataTypeModel;
public DataTypeViewModel()
{
dataTypeModel = new DataTypeModel();
selectedDataType = new DataTypeRepository();
this.dataTypes = dataTypeModel.GetAllDataTypes();
InsertDataTypeCommand = new DelegateCommand(OnInsertDataType);
}
public ObservableCollection<DataTypeRepository> DataTypes
{
get { return dataTypes; }
set
{
if (!EqualityComparer<ObservableCollection<DataTypeRepository>>.Default.Equals(dataTypes,value))
{
dataTypes = value;
RaisePropertyChanged("DataTypes");
}
}
}
public DataTypeRepository SelectedDataType
{
get
{
return selectedDataType;
}
set
{
if (!EqualityComparer<DataTypeRepository>.Default.Equals(selectedDataType, value))
{
selectedDataType = value;
InsertDataTypeCommand.RaiseCanExecuteChanged();
RaisePropertyChanged("SelectedDataType");
}
}
}
public DelegateCommand InsertDataTypeCommand { get; private set; }
private void OnInsertDataType()
{
DataBaseOperationStatusMessage = dataTypeModel.InsertDataType(selectedDataType);
DatabaseOperationComplete = true;
DataTypes = dataTypeModel.GetAllDataTypes();
}
}
请注意,InsertDataTypeCommand 命令在应用程序的另一个选项卡中使用,其中添加了“DataType”。添加数据类型后,用户单击修改选项卡可在组合框列表中查看新的数据类型。但这并没有发生。如果您重新启动应用程序并转到“修改”页面,您可以看到新记录。 问题是即使在使用 INotifyPropertyChange 时,组合框也没有更新。
我在这里错过了什么?
【问题讨论】:
-
ObservableCollection
属性应该是只读的。绝对没有必要使用启用 INPC 的属性来公开它们。如果您想从集合中添加/删除项目,只需使用普通的集合方法。这可能与您的问题有关。 -
我明白你在说什么。所以你建议将 ObservableCollection
的属性更改为 List ?我这样做没有任何效果。 -
在哪里... 如何... 谁.... 我说 what??? 让我再次阅读我的评论... 嗯,属性应该是只读的,是啊……不用了,INPC,好吧……用普通的收集方法,嗯……我相信这里的问题是你高。请把碗递过来。
-
我正在尝试解决这里的问题并寻求帮助。如果你觉得这很有趣,那就去别处大笑吧。
-
试着阅读我的评论并按照它的建议去做。尽量不要做它不建议做的事情。它可能会对你有所帮助。
标签: wpf mvvm data-binding combobox refresh