C# 如何添加默认comboboxitem”--选择用户--

C# 如何添加默认comboboxitem”--选择用户--,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我正在使用MVVM模式开发一个WPF应用程序,我有一个组合框,其中itemssource来自viewmodel,我想在组合框中有一个默认选项,比如-Select user-,最好的方法是什么。 这是我的XAML代码: <ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="5.2,8.2,0,7.8" Grid.Row="5" Width="340" ItemsSource="{Binding Path=Users}" Is

我正在使用MVVM模式开发一个WPF应用程序,我有一个组合框,其中itemssource来自viewmodel,我想在组合框中有一个默认选项,比如-Select user-,最好的方法是什么。 这是我的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>

最简单的方法是在绑定列表/集合中有这样的项目。在视图模型中填充完列表后,只需执行以下操作

myViewModel.Users.Insert(0, "--Select user--");

这是一个伪代码,请调整。

另一种方法是使用触发器,让comboboxitems保持原样

<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,那么您选择的用户项应该与所有其他项一起位于视图模型中的集合中。根据数据项的类型,有几种方法可以实现这一点

如果您只是使用普通字符串,那么很容易。。。只需在第一个索引的数据绑定集合中添加一个字符串:

DataBoundCollection.Insert(0, "--Select user--");
如果数据类型是自定义类,则可以使用用于显示项目名称/标识符的字符串属性来保存该值:

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(...));
然后,您可以使用未设置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>

假设用户是字符串的集合?这就是为什么我写它是一个伪代码,因为我不知道User classyea的规范,所以如果用户是一个对象,你建议添加一个新对象吗?是的,将其作为默认值/空值对象,并在保存集合时检查数据库中是否没有默认值或空值?对我来说这听起来不太好真的取决于Op你说的让comboboxitem在你的ComboboxItems列表中没有附加项是什么意思,只是默认文本。我很困惑,我不明白你说的是什么意思我的意思是,当没有选择项时,上面两种解决方案都会在你的ComboxBox上给你一个文本,当你打开组合框时,你将看不到任何带有文本的项目-选择用户-…这些不是冗长的解决方案,请实现它们,看看它们有什么不同。@PaulB关于这个问题的回答对我有用
<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>