如何添加默认组合框项目“--Select 用户--”
How do I add a default comboboxitem "--Select user--"
我正在使用 MVVM 模式开发一个 WPF 应用程序,我有一个组合框,其中的项目源从视图模型中绑定,我想在组合框中有一个默认选项,如“--Select 用户--”,什么是执行此操作的最佳方法。
这是我的 XAML 代码:
<ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="5.2,8.2,0,7.8" Grid.Row="5" Width="340" ItemsSource="{Binding Path=Users}" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding SelectedUser}" Grid.ColumnSpan="2">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox>
SelectedIndex="0"
<ComboBox.ItemsSource>
<CompositeCollection>
<ListBoxItem>Please Select</ListBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource YOURDATASOURCE}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
最简单的方法是将此类项目绑定 list/collection。在视图模型中完成填充列表后,只需执行
myViewModel.Users.Insert(0, "--Select user--");
伪代码,请自行调整
好吧,另一种方法是使用触发器并让组合框项目保持原样
<Grid>
<ComboBox x:Name="mycombo" ItemsSource="{Binding}"/>
<TextBlock Text="-- Select User --" IsHitTestVisible="False" Visibility="Hidden">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=mycombo,Path=SelectedItem}" Value="{x:Null}">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
或者如果您有能力将组合框的外观更改为看起来像可编辑的组合框,您可以这样做 --
<ComboBox IsEditable="True" IsReadOnly="True" Text="-- Select User --" />
如果您使用的是 MVVM,那么您的 Select 用户 项目应该与所有其他项目一起位于视图模型的集合中。有几种方法可以实现这一点,具体取决于您的数据项的类型。
如果您只是使用普通的 string
s,那么很简单...只需将字符串添加到第一个索引的数据绑定集合中即可:
DataBoundCollection.Insert(0, "--Select user--");
如果您的数据类型是自定义 class,那么您可以使用用于显示项目 name/identifier 的 string
属性 来保存该值:
DataBoundCollection.Insert(0, new YourDataType("--Select user--", null, null, 0, 0));
另一种选择是使 DataBoundCollection
成为基本类型的集合。普通项目应该派生这个基本类型,因此可以添加到这个集合中。默认项可以是另一种派生类型(以便可以不同地显示)也可以添加到同一集合中:
// You could even hard code this text into the DefaultType object alternatively
DataBoundCollection.Add(new DefaultType("--Select user--"));
DataBoundCollection.Add(new YourDataType(...));
DataBoundCollection.Add(new YourDataType(...));
...
DataBoundCollection.Add(new YourDataType(...));
然后您可以使用 DataTemplate
在 ComboBox
中以不同方式显示它们,而 而不是 设置了 x:Key
指令:
<DataTemplate DataType="{x:Type Local:YourDataType}">
<!-- Define how your normal items are displayed here -->
</DataTemplate>
<DataTemplate DataType="{x:Type Local:DefaultType}">
<!-- Define how your default item is displayed here -->
</DataTemplate>
我正在使用 MVVM 模式开发一个 WPF 应用程序,我有一个组合框,其中的项目源从视图模型中绑定,我想在组合框中有一个默认选项,如“--Select 用户--”,什么是执行此操作的最佳方法。 这是我的 XAML 代码:
<ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="5.2,8.2,0,7.8" Grid.Row="5" Width="340" ItemsSource="{Binding Path=Users}" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding SelectedUser}" Grid.ColumnSpan="2">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox>
SelectedIndex="0"
<ComboBox.ItemsSource>
<CompositeCollection>
<ListBoxItem>Please Select</ListBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource YOURDATASOURCE}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
最简单的方法是将此类项目绑定 list/collection。在视图模型中完成填充列表后,只需执行
myViewModel.Users.Insert(0, "--Select user--");
伪代码,请自行调整
好吧,另一种方法是使用触发器并让组合框项目保持原样
<Grid>
<ComboBox x:Name="mycombo" ItemsSource="{Binding}"/>
<TextBlock Text="-- Select User --" IsHitTestVisible="False" Visibility="Hidden">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=mycombo,Path=SelectedItem}" Value="{x:Null}">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
或者如果您有能力将组合框的外观更改为看起来像可编辑的组合框,您可以这样做 --
<ComboBox IsEditable="True" IsReadOnly="True" Text="-- Select User --" />
如果您使用的是 MVVM,那么您的 Select 用户 项目应该与所有其他项目一起位于视图模型的集合中。有几种方法可以实现这一点,具体取决于您的数据项的类型。
如果您只是使用普通的 string
s,那么很简单...只需将字符串添加到第一个索引的数据绑定集合中即可:
DataBoundCollection.Insert(0, "--Select user--");
如果您的数据类型是自定义 class,那么您可以使用用于显示项目 name/identifier 的 string
属性 来保存该值:
DataBoundCollection.Insert(0, new YourDataType("--Select user--", null, null, 0, 0));
另一种选择是使 DataBoundCollection
成为基本类型的集合。普通项目应该派生这个基本类型,因此可以添加到这个集合中。默认项可以是另一种派生类型(以便可以不同地显示)也可以添加到同一集合中:
// You could even hard code this text into the DefaultType object alternatively
DataBoundCollection.Add(new DefaultType("--Select user--"));
DataBoundCollection.Add(new YourDataType(...));
DataBoundCollection.Add(new YourDataType(...));
...
DataBoundCollection.Add(new YourDataType(...));
然后您可以使用 DataTemplate
在 ComboBox
中以不同方式显示它们,而 而不是 设置了 x:Key
指令:
<DataTemplate DataType="{x:Type Local:YourDataType}">
<!-- Define how your normal items are displayed here -->
</DataTemplate>
<DataTemplate DataType="{x:Type Local:DefaultType}">
<!-- Define how your default item is displayed here -->
</DataTemplate>