【发布时间】:2011-04-27 01:39:09
【问题描述】:
以下代码未在我的视图模型中调用 SelectedItem 的属性设置器。
<ComboBox x:Name="cmbGuaranteeType" Margin="5,5,5,5" MinWidth="80"
ItemsSource="{Binding Source={StaticResource guaranteeTypesKey}}"
SelectedItem="{Binding RelativeSource={RelativeSource AncestorType={x:Type wpfToolkit:DataGridRow}}, Path=DataContext.GuaranteeType, Mode=TwoWay}"
/>
视图是绑定到 ViewModel 的 UserControl。 UserControl 有一个 WpfToolkit 数据网格,它绑定到 ViewModel 上的 ObservableCollection。 ObservableCollection 由 List<T> 项组成。上面的组合框绑定到 T 项之一的“GuaranteeType”属性,如下所示:
public GuaranteeType? GuaranteeType
{
get { return _guaranteeType; }
set { _guaranteeType = value; NotifyPropertyChanged(ConstGuaranteeType); }
}
加载 UserControl 时,会调用 GuaranteeType 属性的 getter 并正确设置 SelectedItem。但是,当我单击 ComboBox 并尝试更改 SelectedItem 时,永远不会调用 GuarenteeType 属性的设置器。
此外,我在 XAML 中针对 View 的代码隐藏实现了 SelectionChanged="cmbGuaranteeType_SelectionChanged",当视图加载时,SelectionChanged 方法被调用一次,但当我在尝试更改组合框的选定项时释放鼠标时不会调用它。
【问题讨论】:
标签: wpf data-binding