WPF Combobox的默认值(请选择)

39

你好,我有一个WPF Combobox,显示了一系列的枚举值。以下是代码:

    <ComboBox HorizontalAlignment="Left" 
              Margin="139,299,0,0" 
              VerticalAlignment="Top" 
              ItemsSource="{Binding Source={StaticResource Enum}}"
              Width="78"/> 

然而,当视图被加载时,它显示列表中的第一个枚举值,但我想要它显示“请选择”,那么有没有XAML可以做到这一点(如果需要,在视图中使用C#...)

谢谢


请查看此链接:https://dev59.com/D3E85IYBdhLWcg3wMAfc - ElectricRouge
7个回答

58

所有提供的答案都很好,但我使用以下方法解决了我的问题

<ComboBox SelectedIndex="0">
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ListBoxItem>Please Select</ListBoxItem>
            <CollectionContainer Collection="{Binding Source={StaticResource YOURDATASOURCE}}" />
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>

感谢所有帮助过我的人!


1
这是一个相当不错和聪明的解决方案。 - IbrarMumtaz
你可以使用 ComboBoxItem Visibility="Collapsed">Please Select</ComboBoxItem> 来使选择列表呈现得更好。 - IronHide
3
不错的方法,但问题在于SelectedItem或SelectedValue将会是"System.Windows.Controls.ComboBoxItem: Please Select"。 - Abdulkarim Kanaan

23
在你的组合框中添加这些属性,你可以在组合框上设置默认的“请选择”文本。
<ComboBox IsEditable="True" IsReadOnly="True" Text="Please Select"/>

为了提供更加多功能的解决方案,您可以为下拉列表创建一个水印。


1
我知道这是老问题,但我现在遇到了一些问题。它会显示“请选择”,但一旦我选择一个项目,它就会显示组合框的类/数据类型,而不是我想要显示的属性。 - gdubs
@gdubs 如果您有其他问题,请通过单击提问按钮来提出。 - Hille

15
我用我的做了这件事,对我有效,因为我的物品是静止的。
<ComboBox Name="cbxType" HorizontalAlignment="Left" Margin="116,41,0,0" VerticalAlignment="Top" Width="192">
    <ComboBoxItem Name="create" IsSelected="True">create database</ComboBoxItem>
    <ComboBoxItem Name="update">update database</ComboBoxItem>
</ComboBox>

10
您可以通过以下代码实现该功能:

<Grid>
                <ComboBox
                    MinWidth="120"
                    x:Name="MyCombo"
                    ItemsSource="{Binding FileTypes}"  
                    SelectedItem="{Binding SelectedFileType}"/>
                <TextBlock
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    Visibility="{Binding SelectedItem, ElementName=MyCombo, Converter={StaticResource NullToVisibilityConverter}}"
                    IsHitTestVisible="False"
                    Text="Select Option...  " />
</Grid>

每当您需要上述文本(文本框)时,您可以使用VisibilityConverter将其显示在组合框顶部...

将以下内容添加到您的资源中:

<local:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />

4

当在WPF窗口/用户控件中首次加载/初始化控件时,将ComboBox的默认值设置为"SELL":

<ComboBox x:Name="OrderType" 
          Width="100" Height="20"
          SelectedIndex="1">
         <ComboBoxItem Content="BUY"/>
         <ComboBoxItem Content="SELL"/> 
</ComboBox>

3
将值“请选择”添加到您的EnumCollection中。
在ComboBox样式设置器中设置默认值。
<Style x:Key="ComboStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="SelectedIndex" Value="0"/>
</Style>

XAML:

<ComboBox HorizontalAlignment="Left" 
              Margin="139,299,0,0" 
              Style="{StaticResource ComboStyle}"
              VerticalAlignment="Top" 
              ItemsSource="{Binding Source={StaticResource ComboBox}}"
              Width="78"/> 

谢谢,看起来很合理,但是枚举值来自数据库,所以我不想特别添加“请选择”选项。有什么解决办法吗? - user3428422
@user3428422,您可以查看此线程http://stackoverflow.com/questions/4639533/default-value-for-combobox - Sajeetharan
2
我认为这是一个糟糕的解决方案。更改数据源以更改其Presenter的行为是真正的代码异味。 Krishna的答案要好得多,因为它只涉及对控件的更改。 - ProfK

2

不知道如何在没有代码后台的情况下完成,也许可以使用一些触发器或DataTemplateSelectors…?

使用代码后台:

  1. 添加一个可枚举字符串属性,其中仅包含一个字符串:“请选择”
  2. 在XAML中将ItemsSource设置为该属性,SelectedIndex = 0
  3. DropDownOpened事件中,将ComboBox.ItemsSource设置为您的枚举集合

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接