【问题标题】:WPF ComboBox SelectedValue binding not updated when selection is changed更改选择时未更新 WPF ComboBox SelectedValue 绑定
【发布时间】:2012-05-10 20:32:17
【问题描述】:

我有一个 Telerik RadComboBox 设置如下:

<t:RadComboBox Grid.Column="1" Grid.ColumnSpan="5" Grid.Row="3" x:Name="customer" IsReadOnly="True" IsEditable="True"
               ItemsSource="{Binding Path=Customers, Mode=OneWay}" DisplayMemberPath="Name" SelectedValuePath="Id"
               SelectedValue="{Binding Path=CustomerId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
               SelectionChanged="customer_SelectionChanged_1"/>

组合已正确填充,但如果我单击下拉菜单并从列表中选择一个项目,SelectedValue 属性不会更新。如果我开始输入组合并以这种方式选择一个项目,SelectedValue 属性不会更新。 但是如果我使用鼠标滚轮或箭头键,那么SelectedValue 属性会正确更新。

我在后面的代码中添加了SelectionChanged 事件,因此我可以查看控件的属性(此应用程序使用 MVVM 模式),即使SelectedItem 属性 看起来也是如此 设置为有效项,SelectedValue 属性为空。如果我使用鼠标滚轮向下滚动到某个项目,那么这两个属性都按照我的预期正确设置。

【问题讨论】:

    标签: wpf combobox telerik selectedvalue


    【解决方案1】:

    原来这是因为我的视图模型在没有选择时默认返回Guid.Empty。但由于列表中没有一个项目具有 Id 属性为 Guid.Empty,因此控件会混淆。

    添加属性:SelectedIndex="0" 解决了这个问题。

    【讨论】:

    • 这也让我着迷..我在没有选择任何内容时使用SelectedValue=nullSelectedValuePath 设置为“名称”),这会导致“控件变得混乱”,正如你所说,所以我改用SelectedItem,一切似乎都正常。
    猜你喜欢
    • 2012-05-12
    • 2012-09-25
    • 2013-03-03
    • 2012-06-18
    • 2013-06-09
    • 1970-01-01
    • 2012-06-23
    • 2010-10-12
    • 1970-01-01
    相关资源
    最近更新 更多