你应该通过创建一个风格触发像下面
<ComboBox x:Name="ACombobox" ItemsSource="{Binding Mode=OneWay, Source={StaticResource AList}}"
DisplayMemberPath="TypeName" SelectedValuePath="TypeName"
SelectedValue="{Binding AnObj.Type, Mode=TwoWay}" >
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="{x:Null}">
<Setter Property="SelectedIndex" Value="0" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox>
错误说it does not have a qualifying type name
所以通过创建样式应用到组合框,当你设置做在TargetType="ComboBox"
<ComboBox x:Name="ACombobox" ItemsSource="{Binding AList}"
DisplayMemberPath="TypeName" SelectedValuePath="TypeName"
SelectedValue="{Binding AnObj.Type, Mode=TwoWay}" >
<ComboBox.Resources>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem}" Value="{x:Null}">
<Setter Property="SelectedIndex" Value="0" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Resources>
</ComboBox>
这适用于我。
与静态资源
<Window.Resources>
<x:Array x:Key="StringList" Type="System:String">
<System:String>Line 1</System:String>
<System:String>Line 2</System:String>
<System:String>Line 3</System:String>
<System:String>Line 4</System:String>
</x:Array>
</Window.Resources>
<ComboBox ItemsSource="{StaticResource StringList}" >
<ComboBox.Resources>
<Style TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="SelectedItem" Value="{x:Null}">
<Setter Property="SelectedIndex" Value="0"/>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Resources>
</ComboBox>
阿斌例子:我有一个例外:项目集合必须是使用项目源之前空。 – TheOne
Alist是一个在xaml中定义的静态资源。如果我采取这种风格一切正常,我没有得到任何错误。 – TheOne
尝试代码时遇到同样的问题。 – Kcvin