2

選択したセルのスタイルを WPF DataGrid コントロールで変更しようとしています。

DataGridCellスタイリングとはどう違いDataGridRowますか?以下のさまざまな組み合わせを試しましたが、すべて機能します。ただし、スタイルを設定する必要があるのはまたはのいずれか DataGridCellだけのようですDataGridRow

両方を持つ目的は何ですか?これは、私が彼らの目的を誤解していることを示しています。

スタイルDataGridCell: (自分のコードでは、デフォルトから色を変更します)

 <Style TargetType="{x:Type DataGridCell}">
 <Style.Triggers>
  <Trigger Property="IsSelected" Value="True">
  <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
  <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  </Trigger>
 </Style.Triggers>
 </Style>

スタイルDataGridRow: (自分のコードでは、デフォルトから色を変更します)

 <Style TargetType="{x:Type DataGridRow}">
 <Style.Triggers>
  <Trigger Property="IsSelected" Value="True">
  <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
  <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  </Trigger>
 </Style.Triggers>
 </Style>
4

2 に答える 2

4

DataGridRowは行全体に適用されます。行全体のプロパティを設定する場合は、DataGridRowを使用できます。

各DataGridRowには、DataGridCellのリストが含まれています。そのためのスタイルも定義できます。そうしないと、DataGridRowからスタイルが取得されます。

一般に、後者を指定して、セル間のマージンや境界線などを指定するなど、2つの隣接するセル間の区別を定義します。

于 2012-10-11T02:26:03.140 に答える
2

@abhishekが言ったように...行スタイルと行スタイルであるセルスタイルの両方を使用する必要がある状況があります

<!-- Row Style-->
        <Style x:Key="RowStyle" TargetType="{x:Type dg:DataGridRow}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

次に、使用した選択中に特定のセルを白い背景にする必要があります

<Style x:Key="TimeCell" TargetType="{x:Type dg:DataGridCell}">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Foreground" Value="Black"/>
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

特定の列にスタイルを与えます

<dg:DataGridTemplateColumn Width="120" CellStyle="{StaticResource TimeCell}">

私が言いたいことが明確であることを願っています

于 2013-12-07T14:27:17.880 に答える