使用DataGridrow ControlTemplate中的Visual States

发布于 2025-01-20 21:44:20 字数 3804 浏览 2 评论 0原文

我为DataGridrow创建了一种自定义样式,该样式还更改了我在Visual Studio中获得的控制模板(右键单击控件,然后选择编辑模板的副本(对不起,我没有确切的翻译)。首先,我省略了整个VisualStateManager-stuff,一切都很好。但是,当我想具有交替的行颜色时,我想我必须将VisualStateManager与状态“ normal_alternatingrow”一起使用。不幸的是,我一旦毫无注入了,我就会遇到此错误:

'[unknown]'属性并不指向路径中的依赖项'(0)。(1)。[0] [0]。(2)'

这是我的datagridrow样式使用模板:

<Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
<Setter Property="HeaderStyle" Value="{StaticResource DataGridRowHeaderStyle}"/>
<Setter Property="Background" Value="{StaticResource ControlLightBackgroundBrush}"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="{x:Type DataGridRow}">
      <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" 
              BorderThickness="{TemplateBinding BorderThickness}" 
              Background="{TemplateBinding Background}" SnapsToDevicePixels="True"                  >
        <SelectiveScrollingGrid>
          <SelectiveScrollingGrid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
          </SelectiveScrollingGrid.ColumnDefinitions>
          <SelectiveScrollingGrid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
          </SelectiveScrollingGrid.RowDefinitions>
          <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
          <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" 
                                    SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
          <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
        </SelectiveScrollingGrid>
        <VisualStateManager.VisualStateGroups>
          <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Normal"/>
            <VisualState x:Name="Normal_AlternatingRow">
              <Storyboard>
                <ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border"
                                              Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                  <EasingColorKeyFrame KeyTime="0" Value="Yellow"/>
                </ColorAnimationUsingKeyFrames>                   
              </Storyboard>
            </VisualState>
          </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
      </Border>
    </ControlTemplate>
  </Setter.Value>
</Setter>
<Style.Triggers>
  <Trigger Property="IsNewItem" Value="True">
    <Setter Property="Margin" Value="{Binding NewItemMargin, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
  </Trigger>
  <Trigger Property="IsSelected" Value="True">
    <Setter Property="Background" Value="{StaticResource HighlightBrush}"/>
  </Trigger>
</Style.Triggers>

我以前从未在控制模板中使用视觉状态。

感谢您的帮助!

I created a custom style for a DataGridRow which also changes the control template which I obtained in Visual Studio (right click on the control and chose to edit copy of the template (sorry, I don't have the exact translation)). First I omitted the entire VisualStateManager-stuff and everything worked fine. But when I want to have alternating row colors I guess I have to use the VisualStateManager with the state 'Normal_AlternatingRow'. Unfortunately, I am getting this error as soon as the VisualStateManager is uncommented:

'[Unknown]' property does not point to a dependencyobject in path '(0).(1)[0].(2)'

This is my DataGridRow style with template:

<Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
<Setter Property="HeaderStyle" Value="{StaticResource DataGridRowHeaderStyle}"/>
<Setter Property="Background" Value="{StaticResource ControlLightBackgroundBrush}"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="{x:Type DataGridRow}">
      <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" 
              BorderThickness="{TemplateBinding BorderThickness}" 
              Background="{TemplateBinding Background}" SnapsToDevicePixels="True"                  >
        <SelectiveScrollingGrid>
          <SelectiveScrollingGrid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
          </SelectiveScrollingGrid.ColumnDefinitions>
          <SelectiveScrollingGrid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
          </SelectiveScrollingGrid.RowDefinitions>
          <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
          <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" 
                                    SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
          <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
        </SelectiveScrollingGrid>
        <VisualStateManager.VisualStateGroups>
          <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Normal"/>
            <VisualState x:Name="Normal_AlternatingRow">
              <Storyboard>
                <ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border"
                                              Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
                  <EasingColorKeyFrame KeyTime="0" Value="Yellow"/>
                </ColorAnimationUsingKeyFrames>                   
              </Storyboard>
            </VisualState>
          </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
      </Border>
    </ControlTemplate>
  </Setter.Value>
</Setter>
<Style.Triggers>
  <Trigger Property="IsNewItem" Value="True">
    <Setter Property="Margin" Value="{Binding NewItemMargin, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
  </Trigger>
  <Trigger Property="IsSelected" Value="True">
    <Setter Property="Background" Value="{StaticResource HighlightBrush}"/>
  </Trigger>
</Style.Triggers>

I haven't used visual states in control templates before.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卸妝后依然美 2025-01-27 21:44:20

当前实现假定 Background 属性设置为 GradientBrush

如果 ControlLightBackgroundBrushSolidColorBrush,则以下内容应该有效:

<VisualState x:Name="Normal_AlternatingRow">
    <Storyboard>
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border"
                                      Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)">
            <EasingColorKeyFrame KeyTime="0" Value="Yellow"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

The current implementation assumes that the Background property is set to a GradientBrush.

If ControlLightBackgroundBrush is a SolidColorBrush, the following should work:

<VisualState x:Name="Normal_AlternatingRow">
    <Storyboard>
        <ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border"
                                      Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)">
            <EasingColorKeyFrame KeyTime="0" Value="Yellow"/>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文