【发布时间】:2021-08-12 03:15:39
【问题描述】:
我有一个 ListBox 绑定到一个 List 并且 selectedItem 也绑定到一个 List 通常只有一个条目。所以对于 selectItem 我得到了绑定列表中的第一个:
<ListBox HorizontalAlignment="Left"
ItemsSource="{Binding Definition.DatatypeDefinition.SpecifiedValues}"
SelectedItem="{Binding ObjectValue[0], Mode=TwoWay}"
IsEnabled="{Binding AttributeDefinition.IsEditable, Converter={StaticResource AttributeEditableConverter}}"
Name="dataField">
</ListBox>
我预计如果我选择另一个项目,绑定属性的 set 操作将被触发,但只有 get 操作被触发。这就是我的 ObjectValue 的样子:
public List<EnumValue> Values
{
get
{
NotifyPropertyChanged();
return this.values;
}
}
public override object ObjectValue
{
get => this.Values;
set
{
if (!(value is IEnumerable<EnumValue> enumValues))
{
throw new InvalidOperationException($"The value to set is not an IEnumerable<EnumValue>.");
}
this.values.Clear();
this.values.AddRange(enumValues);
NotifyPropertyChanged();
}
}
为什么我更改Listbox中的SelectedItem时我的set Operation没有启动?没看懂。
【问题讨论】:
标签: c# wpf listbox selecteditem