2

我的 WPF 应用程序中有一个数据网格。我一直在尝试向 datagridrowheader 添加一个切换按钮,我已经设法做到了。我遇到的问题是尝试设置 datagridrowheader 的样式。如果我遗漏了这一行 - RowHeaderStyle="{StaticResource DG_RowHeader}" 我的按钮会正确加载。当我添加此行时,我的按钮没有出现,这是为什么呢?

据我所见, DG_RowHeader 只是为行标题设置样式?我想知道如何将这种样式应用到我的 datagridrowheader 并让我的切换按钮也出现?

我的数据网格

<DataGrid DataContext="{Binding OrderBlock}" 
                  x:Name="dataGridOrders" 
                  ItemsSource="{Binding Orders}"
                  Style="{StaticResource DataGridTemplate}"
                  ColumnHeaderStyle="{StaticResource DG_ColumnHeader}"                                            
                  RowStyle="{StaticResource DG_Row}"
                  CellStyle="{StaticResource DG_Cell}"              
                  RowHeaderStyle="{StaticResource DG_RowHeader}"
                  RowDetailsTemplate="{StaticResource DG_RowDetail}"                      
                  AutoGenerateColumns="False"
                  HorizontalAlignment="Stretch" 
                  VerticalAlignment="Stretch"
                  Background="Silver"
                  RowHeaderWidth="30"                      
                  Margin="25,5,20,15"
                  RowDetailsVisibilityChanged="dataGridOrders_RowDetailsVisibilityChanged">
            <DataGrid.RowHeaderTemplate>                    
                <DataTemplate>
                    <ToggleButton x:Name="RowHeaderToggleButton"                                     
                                  Click="RowHeaderToggleButton_Click"
                                  Cursor="Hand"/>
                </DataTemplate>
            </DataGrid.RowHeaderTemplate>

我的 DataGridRowHeader

<!-- Data Grid row with toggle button -->
    <Style x:Key="DG_RowHeader" TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="Width" Value="35"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
                    <Border x:Name="DGRH_Border"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                SnapsToDevicePixels="True">
                        <Border.Background>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                <GradientStop Offset="0" Color="LightGray"/>
                                <GradientStop Offset="1" Color="WhiteSmoke"/>
                            </LinearGradientBrush>
                        </Border.Background>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我的切换按钮

<!-- Toogle Button -->
    <Style TargetType="ToggleButton" x:Name="rowdetailToggleButton">
        <Setter Property="Padding" Value="0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <Grid>                             
                        <Path x:Name="DefaultPath"
                          VerticalAlignment="Top"
                          Data="M0,0 14,7 0,14 Z"
                          Fill="DarkGray"
                          Stretch="Fill"
                          Margin="6"/>
                        <Path x:Name="CheckedPath"
                          VerticalAlignment="Top"
                          Data="M0,0 14,0 7,14 Z"
                          Fill="DarkGray"
                          Stretch="Fill"
                          Margin="6"
                          Visibility="Collapsed" />
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CheckStates">
                                <VisualState x:Name="Checked">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0" 
                                                                       Storyboard.TargetName="DefaultPath"
                                                                       Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Collapsed</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Duration="0" 
                                                                       Storyboard.TargetName="CheckedPath"
                                                                       Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="CheckedPath" Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)">
                                            <SplineColorKeyFrame KeyTime="0:0:0.2" Value="LightGray" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unchecked" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
4

1 回答 1

2

看起来您忘记显示样式DataGridRowHeader中的内容DG_RowHeader,更准确地说是在内部DGRH_Border

您可以在其中添加 ContentPresenter :

<Border x:Name="DGRH_Border"
        BorderBrush="{TemplateBinding BorderBrush}"
        BorderThickness="{TemplateBinding BorderThickness}"
        SnapsToDevicePixels="True">
    [...]
   <ContentPresenter Content="{TemplateBinding Content}"
                     ContentTemplate="{TemplateBinding ContentTemplate}"/>
</Border>

至于ToggleButtons,您需要:

  • ToggleButton在的声明中指定样式:

(假设您的样式是使用x:Key属性定义的(而是您指定的x:Name)):

   <Style TargetType="ToggleButton" x:Key="rowdetailToggleButton">
  • x:Key通过删除属性将样式设置为切换按钮的默认样式:

无论哪种方式,您都需要确保您的资源字典在按钮声明的位置可用。

于 2013-09-30T13:00:37.373 回答