DataGridColumnHeader的DataTrigger基于DataGridTextColumn标头属性

时间:2015-12-01 09:21:33

标签: wpf datagrid datatrigger datagridtextcolumn datagridcolumnheader

我已为目标类型= DataGridColumnHeader实施过滤器图标样式,并且我在DataGrid附加了样式。现在,过滤器图标应基于DataGridTextColumn标头属性显示。要求是标题="乘客姓名"然后显示过滤器图标

<DataGrid Name="PassengerGrid" AutoGenerateColumns="False"  ColumnHeaderStyle="{StaticResource FilterDataGridColumnHeader}">
     <DataGridTextColumn Binding="{Binding CustomVisitTime}"  Header="Visit Time" CanUserSort="true" Width="90" ElementStyle="{StaticResource DataGridElementStyle}""/>
     <DataGridTextColumn Binding="{Binding PassengerName}"  Header="Passenger Name" CanUserSort="true" Width="90" ElementStyle="{StaticResource DataGridElementStyle}""/>
</DataGrid>

DataGridHeader列的Syle是

<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="FilterDataGridColumnHeader">
        <Setter Property="Padding" Value="2 8"/>
        <Setter Property="Margin" Value="0" />
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="BorderBrush" Value="{StaticResource TheranosMediumGray}"/>
        <Setter Property="BorderThickness" Value="0 0 0 1"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Grid>
                        <Microsoft_Windows_Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="0 3"/>
                                <Path Grid.Column="1" x:Name="SortArrow"  HorizontalAlignment="Right" VerticalAlignment="Center" Width="8" Height="6" Margin="4,0,0,0" Stretch="Fill" Opacity="0.5" Fill="{StaticResource TheranosGray}"
                                            RenderTransformOrigin="0.5,0.4" Visibility="Collapsed" Data="M0,0 L1,0 0.5,1 z" />
                                <Button Grid.Column="2" Command="{Binding Path=DataContext.FilterPopUpCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" Style="{StaticResource TransButtonStyleKey}">
                                    <Button.CommandParameter>
                                        <MultiBinding Converter="{StaticResource MultiValueConverterKey}">
                                            <Binding RelativeSource="{ RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}" />
                                            <Binding Path="Column" RelativeSource="{ RelativeSource Mode=TemplatedParent}" />
                                        </MultiBinding>
                                    </Button.CommandParameter>
                                    <ContentControl Name="autofilter" Visibility="Collapsed"  ContentTemplate="{StaticResource FilterButtonStyleKey}"  Margin="0 0 3 0"></ContentControl>
                                </Button>
                            </Grid>
                        </Microsoft_Windows_Themes:DataGridHeaderBorder>
                        <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left">
                            <Thumb.Style>
                                <Style TargetType="{x:Type Thumb}">
                                    <Setter Property="Width" Value="8"/>
                                    <Setter Property="Background" Value="Transparent"/>
                                    <Setter Property="Cursor" Value="SizeWE"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type Thumb}">
                                                <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </Thumb.Style>
                        </Thumb>
                        <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right">
                            <Thumb.Style>
                                <Style TargetType="{x:Type Thumb}">
                                    <Setter Property="Width" Value="8"/>
                                    <Setter Property="Background" Value="Transparent"/>
                                    <Setter Property="Cursor" Value="SizeWE"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type Thumb}">
                                                <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </Thumb.Style>
                        </Thumb>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="SortDirection" Value="Ascending">
                            <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                            <Setter TargetName="SortArrow" Property="RenderTransform">
                                <Setter.Value>
                                    <RotateTransform Angle="180" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="SortDirection" Value="Descending">
                            <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                        </Trigger>
                        <!--Auto Filter -->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True"></Condition>
                            </MultiTrigger.Conditions>
                            <Setter TargetName="autofilter" Property="Visibility" Value="Visible"/>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:0)

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Column.Header}" Value="Passenger Name"/>
        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"/>
    </MultiDataTrigger.Conditions>
    <Setter TargetName="autofilter" Property="Visibility" Value="Visible"/>
</MultiDataTrigger>