你應該通過創建一個風格觸發像下面
<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