wpf组合框默认值
wpf combo box default value
当 SelectedValue 为 Null 时,我正在尝试将组合框选定项默认设置为索引 = 0。数据触发器有什么问题?
错误:无法识别 SelectedIndex 属性
<ComboBox x:Name="ACombobox" ItemsSource="{Binding Mode=OneWay, Source={StaticResource AList}}"
DisplayMemberPath="TypeName" SelectedValuePath="TypeName"
SelectedValue="{Binding AnObj.Type, Mode=TwoWay}" >
<ComboBox.Triggers>
<DataTrigger Binding="{Binding}" Value="{x:Null}">
<Setter Property="SelectedIndex" Value="0" />
</DataTrigger>
</ComboBox.Triggers>
</ComboBox>
您应该通过创建如下样式触发器来完成
<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>
当 SelectedValue 为 Null 时,我正在尝试将组合框选定项默认设置为索引 = 0。数据触发器有什么问题? 错误:无法识别 SelectedIndex 属性
<ComboBox x:Name="ACombobox" ItemsSource="{Binding Mode=OneWay, Source={StaticResource AList}}"
DisplayMemberPath="TypeName" SelectedValuePath="TypeName"
SelectedValue="{Binding AnObj.Type, Mode=TwoWay}" >
<ComboBox.Triggers>
<DataTrigger Binding="{Binding}" Value="{x:Null}">
<Setter Property="SelectedIndex" Value="0" />
</DataTrigger>
</ComboBox.Triggers>
</ComboBox>
您应该通过创建如下样式触发器来完成
<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>