如何在 UI 上加载组合框后选择"None"作为第一个选定项 - WPF



我似乎无法让我的组合框显示"无";组合框加载后,我将项目插入到复合集合中。如果用户后来决定更改它,我不在乎,但初始负载应该设置";无";首先选择。这是我的XAML

<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox DisplayMemberPath="SName" SelectedItem="{Binding BModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"                                SelectedValuePath="GId"             SelectedValue="{Binding BModel.GId, Mode=TwoWay}"
        HorizontalContentAlignment="Stretch"
        MinWidth="{Binding ElementName=BAssigned, Path=MinWidth}" 
        Style="{StaticResource SPanelComboBox}">
<ComboBox.ItemsSource>
<CompositeCollection>
<emodels:SModel SName="None" GId="-1"/>
<CollectionContainer Collection="{Binding DataContext.BListModels, 
      Source={x:Reference SDataGrid}}"/>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>

<DataGridTemplateColumn.CellEditingTemplate >
<DataTemplate>
<ComboBox SelectedItem="{Binding BModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
        HorizontalContentAlignment="Stretch"
        Style="{StaticResource SPanelComboBox}">
<ComboBox.ItemsSource>
<CompositeCollection>
<emodels:SModel SName="None" GId="-1"/>
<CollectionContainer Collection="{Binding DataContext.BListModels, 
      Source={x:Reference SDataGrid}}"/>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

看看在组合框中设置SelectedIndex和SelectedItem的示例。

<Window.Resources>
<sys:String x:Key="zero">Zero</sys:String>
<spc:StringCollection x:Key="score">
<sys:String>First</sys:String>
<sys:String>Second</sys:String>
<sys:String>Third</sys:String>
</spc:StringCollection>
<CompositeCollection x:Key="strings">
<StaticResource ResourceKey="zero"/>
<CollectionContainer Collection="{Binding Mode=OneWay, Source={StaticResource score}}"/>
</CompositeCollection>
</Window.Resources>
<StackPanel Orientation="Horizontal">
<!--Only index specified-->
<ComboBox Width="100" VerticalAlignment="Top" 
ItemsSource="{Binding Mode=OneWay, Source={StaticResource strings}}"
SelectedIndex="0" />
<!--Only SelectedItem set-->
<ComboBox Width="100" VerticalAlignment="Top" 
ItemsSource="{Binding Mode=OneWay, Source={StaticResource strings}}"
SelectedItem="First" />
<!--SelectedIndex and SelectedItem set to the same item-->
<ComboBox Width="100" VerticalAlignment="Top" 
ItemsSource="{Binding Mode=OneWay, Source={StaticResource strings}}"
SelectedIndex="0"
SelectedItem="Zero" />
<!--SelectedIndex and SelectedItem set to different items-->
<ComboBox Width="100" VerticalAlignment="Top" 
ItemsSource="{Binding Mode=OneWay, Source={StaticResource strings}}"
SelectedIndex="0"
SelectedItem="First" />
</StackPanel>

示例显示SelectedItem的优先级高于SelectedIndex。如果同时设置了这两个属性,则将使用SelectedItem。

在代码中,BModel属性包含BListModels集合中的项或为null
如果使用SelectedIndex=0,则需要将SelectedItem设置为SName="无">
由于SelectedItem具有不同的含义,将忽略设置SelectedIndex=0。

我看到了两种解决方案
第一种是初始化BModel属性时将其值设置为非null,但SName=";无">
我不能教你怎么做
由于您没有给出足够的代码,我需要了解您是如何实现具有BModel属性的元素的BListModels、emodels:SModel集合类型的。

第二种变体是在加载ComboBox之后设置SelectedIndex值
我将在这里使用C#代码向您展示一个变体,但您也可以使用AP属性、Behavior或其他任何东西来实现它。

<StackPanel>
<ComboBox Width="100" VerticalAlignment="Top" 
ItemsSource="{Binding Mode=OneWay, Source={StaticResource strings}}"
SelectedIndex="0"
SelectedItem="First" Loaded="ComboBox_Loaded" />
<x:Code>
<![CDATA[
private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
((Selector)sender).SelectedIndex = 0;
}
]]>
</x:Code>
</StackPanel>

最新更新