【问题标题】:ComboBox with fixed Header具有固定标题的组合框
【发布时间】:2017-01-16 08:07:15
【问题描述】:

我需要在我的ComboBox 中显示一个默认文本,当用户选择Combobox 的一个项目时,该文本也不能更改,实际上为此我创建了这个结构:

<ComboBox ItemsSource="{Binding AvailableNations}" Width="160" Height="55" Margin="0, 0, 0, 15" 
           Text="Select Countries" IsEditable="True">
     <ComboBox.ItemTemplate>
         <DataTemplate>
             <CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Item.Name}" />
         </DataTemplate>
     </ComboBox.ItemTemplate>
</ComboBox>

此显示为默认文本Select Countries,但如果我选择一个项目,默认文本将消失并显示所选项目,我该如何解决这个问题?

【问题讨论】:

  • ComboBox 旨在显示所选项目。为什么不使用TextBlock 在它旁边显示"Select country:"?为什么它必须是ComboBox 的一部分?通常当ComboBox 没有任何选择时使用此行为(显示提示)。但是一旦有了提示,就不再需要提示并显示选定的值。
  • '因为我需要在组合框中显示默认文本而不是所选项目,用户可以在我的组合框中选择更多项目..
  • 您可以在处理选择后将Text 设置为"Select Countries"(我假设您以某种方式向用户显示多选,必须打开下拉菜单才能看到它听起来不像好的设计)。您是否尝试过寻找现有的多选ComboBoxes?例如。 here.
  • 我自己写了一个类来管理多选
  • 我的理解是否正确,您希望文本在空白时显示Select Countries :,在选择项目时显示Select Countries : CAN, US, ETC

标签: c# wpf xaml combobox


【解决方案1】:

您可以使用组合模板 (ref post)

<Window.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="NormalItemTemplate" >
            <CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Item.Name}" />
        </DataTemplate>
        <DataTemplate x:Key="SelectionBoxTemplate" >
            <TextBlock>Select Countries</TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="CombinedTemplate">
            <ContentPresenter x:Name="Presenter"
                   Content="{Binding}"
                   ContentTemplate="{StaticResource NormalItemTemplate}" />
            <DataTemplate.Triggers>
                <DataTrigger
                        Binding="{Binding RelativeSource={RelativeSource FindAncestor,ComboBoxItem,1}}"
                        Value="{x:Null}">
                    <Setter TargetName="Presenter" Property="ContentTemplate"
                            Value="{StaticResource SelectionBoxTemplate}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <ComboBox ItemsSource="{Binding AvailableNations}"                  
              SelectedItem="{Binding SelectedNation}"
              ItemTemplate="{StaticResource CombinedTemplate}"
              Width="160" Height="55" Margin="0, 0, 0, 15" >
    </ComboBox>
</Grid>

原始答案中描述了它的工作方式。请注意,建议的解决方案仅在 IsEditable 设置为 false 时才有效,我认为在您的情况下这不会成为问题。其次,为了在启动时显示文本,我绑定了 SelectedItem(例如,绑定到集合中的第一个项目)。

【讨论】:

    【解决方案2】:

    comments 看来,您似乎只想始终显示Select Countries 文本,即使选择了一个项目。

    我个人会走简单的路线,在ComboBox 上放置一个TextBox,并使用透明前景色隐藏组合框的显示文本。

    这是一个演示它的简单示例:

    <Grid>
        <ComboBox SelectedIndex="1" Foreground="Transparent">
            <ComboBox.Resources>
                <Style TargetType="{x:Type ComboBoxItem}">
                    <!-- Make sure ComboBoxItems don't have transparent text -->
                    <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.ControlTextBrushKey}}" />
                </Style>
            </ComboBox.Resources>
            <ComboBoxItem>Test 1</ComboBoxItem>
            <ComboBoxItem>Test 2</ComboBoxItem>
            <ComboBoxItem>Test 3</ComboBoxItem>
        </ComboBox>
    
        <TextBlock Text="Select Countries" Margin="4,3" IsHitTestVisible="False" />
    </Grid>
    

    以及结果(注意SelectedIndex = 1

    我确信还有其他方法,例如覆盖它绘制显示文本的方式,或更改控件模板,但这对我来说似乎是最简单的。

    【讨论】:

    • 感谢您的回答,非常感谢 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2013-03-26
    • 2010-10-15
    相关资源
    最近更新 更多