0

DataGridコントロールを使用し、パトカーの警察官が使用する WPF アプリケーションがあります。マージされた辞書を使用して、昼と夜の「モード」を実装します。プログラムを 2 つの間で切り替えると、カラー パレットが変化します。このアプリケーションは、会社が作成した特別なセンサーからデータを収集し、役員に表示します。

対象者のDataGrid挙動がおかしくなっています。プログラムが最初に開始されたとき、最初は空です。データが収集されると、行が に追加されますDataGrid。プログラムを開始すると、最初はデイ モードになっています。問題は、最初の行の背景がコントロールのナイト モード カラーに変更されないことです。デイモードカラーの白のままです。デイモードとナイトモードを交互に切り替えても、白のままです。

DataGridこれは、適切な色を持ち、色を適切に切り替える行の後に追加される行とは異なります。

DataGridRowクラスの App.xaml で定義したスタイルは次のとおりです。

<Application x:Class="MyApplication.App"
     . . .>

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MyApplication;component/DayTime.xaml" />

                <ResourceDictionary>
                    . . .
                    <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="BorderBrush"     Value="{DynamicResource DataBorder}" />
                    <Setter Property="Background"      Value="{DynamicResource DataBackground}" />
                    <Setter Property="Foreground"      Value="{DynamicResource DataForeground}" />
                <Style.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="Background"  Value="{DynamicResource DataBackground}" />
                        <Setter Property="BorderBrush" Value="{DynamicResource DataBorderFocused}" />
                        <Setter Property="Foreground"  Value="{DynamicResource DataForeground}" />
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="Background"  Value="{DynamicResource DataBackground}" />
                        <Setter Property="BorderBrush" Value="{DynamicResource DataBorderFocused}" />
                        <Setter Property="Foreground"  Value="{DynamicResource DataForeground}" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background"  Value="{DynamicResource DataBackgroundSelected}" />
                        <Setter Property="BorderBrush" Value="{DynamicResource DataBorderSelected}" />
                        <Setter Property="Foreground"  Value="{DynamicResource DataForegroundSelected}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
            . . .
                </ResourceDictionary>
            </ResourceDictionary>
        </ResourceDictionary>
    </Application.Resources>
</Application>

プログラムで Snoop を実行しDataGridRow、問題のプロパティにドリルダウンすると、Backgroundプロパティの値は白 (#FFFFFFFF) で、値のソースは "DefaultStyle" に設定されます。ただし、これは私が定義したスタイルではないようです。デイ モードに切り替えて白に戻しても変わらないからです。これは Microsoft によって定義された既定のスタイルであり、私のスタイルはまったく使用されていないと思います。DataGridただし、最初に空だった場合に挿入された最初の行のみ。

後続の行では、[値のソース] 列に「ParentTemplate」と表示されます。ナイトモードを切り替えると背景色が適切に変化するため、これは私のスタイルに違いありません。

のすべての行が正しくなるようにこれを修正するにはどうすればよいDataGridですか?

編集:

DataGrid完全を期すために、役立つ場合に備えて、コントロールで使用されるスタイルを次に示します。

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="Background"                    Value="{DynamicResource DataBackground}" />
    <Setter Property="Foreground"                    Value="{DynamicResource TextForeground}" />
    <Setter Property="BorderBrush"                   Value="{DynamicResource DataBorder}" />
    <Setter Property="BorderThickness"               Value="1" />
    <Setter Property="RowDetailsVisibilityMode"      Value="VisibleWhenSelected" />
    <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
    <Setter Property="ScrollViewer.PanningMode"      Value="Both" />
    <Setter Property="Stylus.IsFlicksEnabled"        Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGrid}">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        Padding="{TemplateBinding Padding}"
                        SnapsToDevicePixels="True">
                    <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
                        <ScrollViewer.Template>
                            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Button Command="{x:Static DataGrid.SelectAllCommand}"
                                            Focusable="false"
                                            Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}"
                                            Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                            Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                    <DataGridColumnHeadersPresenter Grid.Column="1"
                                                                    x:Name="PART_ColumnHeadersPresenter"
                                                                    Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
                                                            CanContentScroll="{TemplateBinding CanContentScroll}"
                                                            Grid.ColumnSpan="2"
                                                            Grid.Row="1" />
                                    <ScrollBar x:Name="PART_VerticalScrollBar"
                                               Grid.Column="2"
                                               Maximum="{TemplateBinding ScrollableHeight}"
                                               Orientation="Vertical"
                                               Grid.Row="1"
                                               Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                                               Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
                                               ViewportSize="{TemplateBinding ViewportHeight}"
                                               MinWidth="45" Width="50" />
                                    <Grid Grid.Column="1"
                                          Grid.Row="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <ScrollBar x:Name="PART_HorizontalScrollBar"
                                                   Grid.Column="1"
                                                   Maximum="{TemplateBinding ScrollableWidth}"
                                                   Orientation="Horizontal"
                                                   Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                                                   Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
                                                   ViewportSize="{TemplateBinding ViewportWidth}" />
                                    </Grid>
                                </Grid>
                            </ControlTemplate>
                        </ScrollViewer.Template>
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsGrouping" Value="true">
            <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
        </Trigger>
    </Style.Triggers>
</Style>

編集:

経験者として、このメンバー変数を問題のあるウィンドウのコード ビハインドに追加しました。

private static Style dataGridRowStyle = null;

次に、このコードをウィンドウのコンストラクターに追加しました。

if ( dataGridRowStyle == null ) {
    dataGridRowStyle = FindResource( typeof( DataGridRow ) ) as Style;
    MyGrid.RowStyle = dataGridRowStyle;
}

こうすることで、 に追加されたすべての行DataGridが元のデフォルト スタイルを持つことがわかりました。これは、上記のコードをLoadedイベント ハンドラーに移動したときにも発生しました。

次に、上記のコードを削除Styleし、app.xaml ファイルの定義に x:Key 属性を追加しました。次に、この属性をDataGridコントロールの定義に追加しました。

RowStyle={DynamicResource MyDataGridRowStyle}

これで、すべての行が私のスタイルになりました。TargetTypeこれは素晴らしいことですが、すべての行に適用するには、属性のみでスタイルを宣言するだけで十分だと思いました。なぜそうしなかったのですか?

4

1 に答える 1

1

何度も頭をかきむしり、心を痛め、ウェブを検索した後、私は自分のプログラムで何が起こっているのかをついに理解しました。

私のプログラムは、マージされた辞書を使用して昼モードと夜モードを実装しています。このチュートリアル で説明されているように、マージされた辞書が問題の原因であることが判明しました。

修正は、ルート ディクショナリに既定のスタイルを配置することです。実際、私のコードでは、タグ内のリソース ディクショナリにすべてのテンプレートが含まれていました。それらを 1 レベル上に移動したところ、WPF は最初の行とすべての行で既定のテンプレートを検出するようになりました。

于 2013-03-27T03:58:22.833 に答える