WPF:将Combobox添加到GridViewColumn标头中

时间:2018-02-28 13:54:17

标签: wpf styles

所以在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的{​​{1}}就行了。

如何使用此visible并显示我的style Header

1 个答案:

答案 0 :(得分:0)

尝试这种风格

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