【问题标题】:Change of SelectedItem triggers get instead of set更改 SelectedItem 触发器获取而不是设置
【发布时间】: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


    【解决方案1】:

    为什么我更改Listbox中的SelectedItem时我的set Operation没有启动?

    因为你没有绑定到ObjectValue 属性:

    SelectedItem="{Binding ObjectValue[0]}"
    

    您绑定到ObjectValue 返回的任何值中的第一个值,因此永远不会调用ObjectValue 的设置器。

    SelectedItem="{Binding ObjectValue}" binds to ObjectValue` 并在选择新项目时尝试调用其设置器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-24
      • 2017-10-06
      • 2016-06-15
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多