我遇到与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>
这是ViewModel:
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命令用于应用程序的另一个选项卡,其中&#34; DataType&#34;被添加。添加DataType用户后,单击“修改”选项卡以在组合框列表中查看新的DataType。但这不会发生。如果您重新启动应用程序并转到“修改”页面,则可以看到新记录。 问题是即使使用INotifyPropertyChange,Combobox也没有更新。
我在这里缺少什么?
答案 0 :(得分:0)
尝试更新您的绑定以包含更新触发器...
SelectedItem="{Binding Path=SelectedDataType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
答案 1 :(得分:0)
我猜想以下行不会导致集合更新:
DataTypes = dataTypeModel.GetAllDataTypes();
从您的代码看起来,您可以将新项目插入数据库或任何地方,然后运行加载数据库中所有项目的方法。当您插入数据库时,最好不要运行该方法,以创建相应的DataTypeRepository项并将其添加到集合中。
this.DataTypes.Add(new DataTypeRepository { property1= "somedata"});
这只是意味着如果你在一个巨大的物品清单中添加一个新物品,你将不必重新加载所有物品。
您可能还应该将您的itemsource更新为双向绑定(除非您正在执行将在评论中建议的readonly)并将updatetrigger更改为属性(默认情况下,我认为其焦点丢失)