【问题标题】:WPF Combobox DefaultValue (Please Select)WPF 组合框默认值(请选择)
【发布时间】:2014-04-17 10:31:47
【问题描述】:

您好,我有一个显示枚举列表的 WPF 组合框。代码如下。

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

但是,当视图被加载时,它会显示列表中的第一个枚举,但我希望它显示“请选择”,所以是否有 XAML 来执行此操作(如果需要,视图中的 C#..)

谢谢

【问题讨论】:

标签: c# wpf xaml combobox


【解决方案1】:

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

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

感谢所有帮助过的人!

【讨论】:

  • 这是一个非常好的和聪明的解决方案。
  • 你可以使用ComboBoxItem Visibility="Collapsed"&gt;Please Select&lt;/ComboBoxItem&gt;这将使选择列表呈现得很好
  • 好方法,但问题是 SelectedItem 或 SelectedValue 将是“System.Windows.Controls.ComboBoxItem: Please Select”
【解决方案2】:

将这些属性添加到您的组合框,您可以在组合框上设置默认的“请选择”文本。

<ComboBox IsEditable="True" IsReadOnly="True" Text="Please Select"/>

对于更通用的解决方案,您可以为组合框创建水印

【讨论】:

  • 我知道这是旧的,但我遇到了问题。它会显示“请选择”,但是一旦我选择了一个项目,它就会显示组合框的类/数据类型,而不是我想要显示的属性
  • @gdubs 如果您还有其他问题,请点击 按钮提问。
【解决方案3】:

我用我的做这个,对我有用,因为我有静态项目。

<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>

【讨论】:

    【解决方案4】:

    您可以使用以下代码实现:

    <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" />
    

    【讨论】:

      【解决方案5】:

      将值“请选择”添加到您的 EnumCollection

      在组合框样式设置器中设置默认值

      <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 你可以查看这个帖子stackoverflow.com/questions/4639533/default-value-for-combobox
      • 我认为这是一个糟糕的解决方案。更改数据源以更改其演示者的行为是一种真正的代码异味。 Krishna 的回答要好得多,因为它只涉及控制的变化。
      【解决方案6】:

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

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

      【讨论】:

        【解决方案7】:

        不知道如何在没有代码隐藏的情况下执行此操作,可能是一些触发器或 DataTemplateSelectors...?

        在代码隐藏中:

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

        【讨论】:

          猜你喜欢
          • 2022-01-14
          • 2016-11-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-05
          • 2012-05-01
          • 1970-01-01
          相关资源
          最近更新 更多