我是WPF的新手,请耐心等待。我在我的WPF窗口上有一个ComboBox,ItemSource属性绑定到一个字符串属性列表(国家)和SelectedItem绑定到一个字符串属性(SelectedCountry)。这两个属性都在代码后面 - 我将DataContext设置为“this”(即Window)。WPF默认值(使用样式触发器)绑定
组合框XAML是:
<ComboBox Name="CountryComboBox"
VerticalAlignment="Center"
Width="200"
ItemsSource="{Binding Path=Countries, Mode=OneTime}"
SelectedItem="{Binding Path=SelectedCountry, Mode=TwoWay}">
</ComboBox>
我想有一个默认的“ - 请选择 - ”时未选择项目时显示的选项,因此我把下面的XAML中的App.xaml:
<Style TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="SelectedItem" Value="{x:Null}">
<Setter Property="IsEditable" Value="true" />
<Setter Property="IsReadOnly" Value="true" />
<Setter Property="Text" Value="- Please Select -" />
</Trigger>
</Style.Triggers>
</Style>
当我的窗口第一次显示时,组合框确实有“ - 请选择 - ”文本按预期。然后,当我在组合框中选择一个值时,SelectedCountry被适当填充,但是当我将SelectedCountry属性赋值为“null”时,当我期望它返回到“ - 请选择 - ”。我究竟做错了什么?
谢谢。
尝试添加了当 ComboBox.SelectedValue == NULL触发 – 2013-03-15 18:37:20