WPF:将Combobox添加到GridViewColumn标头中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF:将Combobox添加到GridViewColumn标头中相关的知识,希望对你有一定的参考价值。

所以在ListView.View里面我有这个Style而不是我用来移除Header边界:

<GridView ColumnHeaderContainerStyle="{StaticResource ListViewHeaderDefaultStyle}">

</GridView>

样式

<Style x:Key="ListViewHeaderDefaultStyle" TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                    <Border BorderThickness="0,0,0,1" BorderBrush="{DynamicResource GridViewColumnHeaderBorderBrushColor}" Background="Transparent">
                        <TextBlock x:Name="ContentHeader"
                                   Text="{TemplateBinding Content}"
                                   Padding="0,5,0,0"
                                   Width="{TemplateBinding Width}"
                                   TextAlignment="Left"
                                   FontSize="13"
                                   Margin="5,0,0,0"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Foreground" Value="{DynamicResource GridViewColumnHeaderForegroundColor}"/>
        <Setter Property="FontFamily" Value="{DynamicResource applicationFontFamily}"/>
        <Setter Property="FontSize" Value="{DynamicResource GridViewColumnHeaderFontSize}"/>
    </Style>

在我的Column Header里面我想添加Combobox

<GridViewColumn Width="100" CellTemplate="{StaticResource ComboBoxDataTemplate}">
          <GridViewColumn.Header>
                   <StackPanel>
                       <ComboBox Width="90"
                                 SelectedIndex="0"
                                 SelectionChanged="ComboBox_SelectionChanged"
                                 Margin="0,0,0,0">
                                 <ComboBoxItem Content="   -- Select --"/>
                                 <ComboBoxItem Content="File"/>
                                 <ComboBoxItem Content="Name"/>
                                 </ComboBox>
                    </StackPanel>
          </GridViewColumn.Header>

但因为我使用的Style包含TextBlock我看不到这个Combobox。如果我删除了Style ListViewHeaderDefaultStyle这工作正常,但后来我所有的Header borders,我想再次删除visible

我如何使用这个style并显示我的Header Combobox

答案

尝试这种风格

   <Style x:Key="ListViewHeaderDefaultStyle" TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                    <Border BorderThickness="0,0,0,1" BorderBrush="{DynamicResource GridViewColumnHeaderBorderBrushColor}" Background="Transparent">
                        <Grid>
                            <TextBlock x:Name="ContentHeader"
                               Text="{TemplateBinding Content}"
                               Padding="0,5,0,0"
                               Width="{TemplateBinding Width}"
                               TextAlignment="Left"
                               FontSize="13"
                               Margin="5,0,0,0">
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="Visibility" Value="Visible"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding ContentPresenter}" Value="{x:Null}">
                                                <Setter Property="Visibility" Value="Collapsed"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                            <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Foreground" Value="{DynamicResource GridViewColumnHeaderForegroundColor}"/>
        <Setter Property="FontFamily" Value="{DynamicResource applicationFontFamily}"/>
        <Setter Property="FontSize" Value="{DynamicResource GridViewColumnHeaderFontSize}"/>
    </Style>

以上是关于WPF:将Combobox添加到GridViewColumn标头中的主要内容,如果未能解决你的问题,请参考以下文章

WPF C# 在运行时将文本和图像添加到 ComboBox

WPF 将集合绑定到 ComboBox 并选择一个项目

将 wpf:ComboBox 绑定到 XAML 中的静态类

WPF将comboBox绑定到List

WPF MVVM 将 ComboBox 绑定到 Datagrid 选定项

WPF中,ComboBox值显示问题