列标题样式

时间:2012-09-07 20:00:29

标签: wpf

您好我在尝试创建样式并将其应用于DataGrid标题时出现以下问题,获取以下错误

  

DependencyProperty.UnsetValue}'不是属性'ColumnHeaderStyle'的有效值。 “

他没有看到这是如此,如果有人能给我一个想法,我会很感激留下网格代码和风格。 -

  <!-- ************** DATAGRID ************ -->
<Style x:Key="DataGridFloatingWindow" TargetType="{x:Type DataGrid}">
    <Setter Property="AutoGenerateColumns" Value="False" />
    <Setter Property="CanUserAddRows" Value="False" />
    <Setter Property="CanUserReorderColumns" Value="False" />
    <Setter Property="CanUserResizeColumns" Value="False" />
    <Setter Property="CanUserResizeRows" Value="False" />
    <Setter Property="CanUserSortColumns" Value="False" />
    <Setter Property="IsReadOnly" Value="True" />
    <Setter Property="SelectionUnit" Value="FullRow" />
    <Setter Property="SelectionMode" Value="Extended" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Background" Value="#fcfcfc" />
    <Setter Property="GridLinesVisibility" Value="Horizontal" />
    <Setter Property="VerticalGridLinesBrush" Value="Transparent" />
    <!--<Setter Property="HorizontalGridLinesBrush"/>-->
    <Setter Property="ColumnHeaderStyle" Value="{StaticResource DataGridHeaderFloatingWindow}" >

        <!--<Setter.Value>
            <LinearGradientBrush StartPoint="0,0" EndPoint="3,0" MappingMode="Absolute" SpreadMethod="Repeat">
                <GradientStop Offset="0" Color="#dedede"/>
                <GradientStop Offset="0.5" Color="#dedede"/>
                <GradientStop Offset="0.5" Color="Transparent"/>
                <GradientStop Offset="1" Color="Transparent"/>
            </LinearGradientBrush>
        </Setter.Value>-->
    </Setter>



    <Setter Property="RowStyle">
        <Setter.Value>
            <Style TargetType="{x:Type DataGridRow}">
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" Value="#dcdcdc" />
                        <Setter Property="BorderThickness" Value="0.50"></Setter>
                        <Setter Property="BorderBrush" Value="#787676"></Setter>
                        <Setter Property="Foreground" Value="#6e6e6e"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>


    <Setter Property="CellStyle">
        <Setter.Value>
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="FontFamily" Value="{DynamicResource KozukaGothicPro}" />
                <Setter Property="FontSize" Value="10" />
                <Setter Property="Foreground" Value="#6e6e6e" />
                <Setter Property="FontWeight" Value="Medium" />
                <Setter Property="Padding" Value="10 5 10 7" />

                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="BorderThickness" Value="0"></Setter>
                        <Setter Property="Background" Value="#dcdcdc" />
                        <Setter Property="Foreground" Value="#040404"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>
</Style>


<Style x:Key="DataGridHeaderFloatingWindow" 
       TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="MinWidth" Value="0" />
    <Setter Property="MinHeight" Value="28" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <ContentPresenter Margin="6,3,6,3" VerticalAlignment="Center" />
                    <Path x:Name="SortArrow" Visibility="Collapsed" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill" 
                          Grid.Column="1" Width="8" Height="6" Fill="Red" Margin="0,0,8,0" 
                          VerticalAlignment="Center" RenderTransformOrigin="0.5,0.4" />
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                            <Condition Property="SortDirection" Value="{x:Null}" />
                        </MultiTrigger.Conditions>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="true" />
                            <Condition Property="SortDirection" Value="{x:Null}" />
                        </MultiTrigger.Conditions>
                    </MultiTrigger>

                    <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>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

 <DataGrid x:Name="GeographicZones" Style="{DynamicResource DataGridFloatingWindow}" >
                    <DataGrid.Columns>
                        <DataGridTemplateColumn >
                            <DataGridTemplateColumn.Header>
                                <CheckBox x:Name="CheckBoxSelectAll"></CheckBox>
                            </DataGridTemplateColumn.Header>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{Binding Source={x:Reference CheckBoxSelectAll}, Path=IsChecked, Mode=OneWay}"></CheckBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Button Style="{StaticResource GridEditButtonStyle}" Content="{StaticResource GridEditDetailNormal}"></Button>
                                        <Button Style="{StaticResource GridViewButtonStyle}" Content="{StaticResource GridViewDetailNormal}"></Button>
                                    </Grid>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTextColumn Header="{DynamicResource FW_General_Name}" Binding="{Binding Path=Name}" />
                        <DataGridTextColumn Header="{DynamicResource FW_General_Code}" Binding="{Binding Path=Code}" />
                        <DataGridTemplateColumn Header="{DynamicResource FW_General_Enabled}">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <ContentPresenter Content="{StaticResource GridViewEnabled}" Style="{StaticResource DataGridIconOnEnabled}" />
                                        <ContentPresenter Content="{StaticResource GridViewDisabled}" Style="{StaticResource DataGridIconOnDisabled}" />
                                    </Grid>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

1 个答案:

答案 0 :(得分:1)

将此行中的StaticResource替换为DynamicResource -

<Setter Property="ColumnHeaderStyle"
        Value="{StaticResource DataGridHeaderFloatingWindow}" >

<Setter Property="ColumnHeaderStyle"
        Value="{DynamicResource DataGridHeaderFloatingWindow}" >

或者,将样式DataGridHeaderFloatingWindow移到xaml中DataGridFloatingWindow上方,以便解析引用。

使用DynamicResource时,将在加载dataGrid时在运行时解析引用。鉴于StaticResource引用需要在加载xaml时解析,因此任何StaticResource used in the xaml should be defined above its usage