我有一个使用这样的业务对象的WPF应用程序:
public class MyBusinessObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
private ObservableCollection<Widget> _myWidgets;
public ObservableCollection<Widget> MyWidgets
{
get { return _myWidgets; }
set
{
if (_myWidgets == value)
{
return;
}
_myWidgets = value;
OnPropertyChanged(new PropertyChangedEventArgs("MyWidgets"));
}
}
private Widget _selectedWidget;
public Widget SelectedWidget
{
get { return _selectedWidget; }
set
{
if (_selectedWidget == value)
{
return;
}
_selectedWidget = value;
OnPropertyChanged(new PropertyChangedEventArgs("SelectedWidget"));
}
}
// more properties here
public MyBusinessObject()
{
Widgets = new ObservableCollection<Widget>();
// 10 widget objects are instantiated, given names "Widget #1" - "Widget #10" and added to Widgets
this.SelectedWidget = Widgets.Where(item => item.Name == "Widget #1").FirstOrDefault();
}
每个Widget都有一个.Name属性和一个.Description属性。 Widget类的定义如下:
public class Widget
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name == value)
{
return;
}
_name = value;
OnPropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
private string _description;
public string Description
{
get { return _description; }
set
{
if (_description == value)
{
return;
}
_description = value;
OnPropertyChanged(new PropertyChangedEventArgs("Description"));
}
}
public Widget(string name)
{
this.Name = name;
this.Description = string.Empty;
}
在我的用户界面中,我有一个组合框,可以绑定到我的ObservableCollection小部件,如下所示:
MyBusinessObject myBusinessObject = new MyBusinessObject();
public MainWindow()
{
InitializeComponent();
this.DataContext = myBusinessObject;
selectedWidgetComboBox.ItemsSource = myBusinessObject.Widgets;
selectedWidgetComboBox.SelectedValuePath = "Name";
}
这是组合框的XAML:
<ComboBox x:Name="selectedWidgetComboBox" Width="200" SelectedItem="{Binding SelectedWidget, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}: {1}">
<Binding Path="Name"/>
<Binding Path="Description" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
正如您所看到的,在我的组合框中,我一起显示小部件的名称和描述。当我的用户从组合框中选择了一个小部件时,我需要允许他们编辑小部件的.Description。我通过一个小对话框,然后更新.Description属性。不幸的是,我在让组合框显示新的更新描述时遇到了问题。
当我更新所选小部件的.Description属性时,组合框不会像我期望的那样更新。以下是我所看到的行为的描述:
假设所选小部件的.Name为“Widget#1”。默认情况下,.Description属性设置为string.empty。所以这在我的组合框中显示为“Widget#1:”。然后我编辑描述并使用以下代码将其设置为“ABC”:
myBusinessObject.SelectedWidget.Description =“ABC”;
完成此操作后,组合框仍会显示“Widget#1:”。如果我展开组合框下拉菜单并选择另一个小部件(比如“Widget#3”),然后再返回并再次选择“小部件#1”,然后我会看到“小部件#1:ABC”显示在组合框中。然而,即使组合框现在显示“Widget#1:ABC”,如果我展开下拉列表,我仍然只看到“Widget#1:”。我猜这可能与我的XAML中的ComboBox.ItemTemplate的DataTemplate有关,但我不确定。