从XAML

时间:2017-03-03 19:35:25

标签: c# wpf xaml datagrid

我在XAML文件中的WPF应用程序中创建了很多DataGrids。

通常,我制作的每个DataGrid都是这样的:

<DataGrid x:Name="DamageTakenData" HorizontalAlignment="Left" VerticalAlignment="Top" Width="506" Height="172" Canvas.Left="30" Canvas.Top="228">
                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Row}" Value="1">
                                <Setter Property="Foreground" Value="Red" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="2">
                                <Setter Property="Foreground" Value="Orange" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="3">
                                <Setter Property="Foreground" Value="Orange" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="4">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="5">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="6">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
         </DataGrid>

我有大约6或7个DataGrids,每个都在不同的标签中。每个DataGrid都具有相同的DataGrid.CellStyle

<DataGrid x:Name="PlayerDeathData" HorizontalAlignment="Left" Margin="27,35,0,0" VerticalAlignment="Top" Height="155" Width="626" Grid.ColumnSpan="2">
                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Row}" Value="1">
                                <Setter Property="Foreground" Value="Red" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="2">
                                <Setter Property="Foreground" Value="Orange" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="3">
                                <Setter Property="Foreground" Value="Orange" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="4">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="5">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="6">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
            </DataGrid>

如何删除指定DataGrid.CellStyle的重复代码?

尝试

看看这个问题:Prevent Duplicate Code In XAML,我尝试过类似的东西。我创建了一个静态资源:

<Window.Resources>
    <Style TargetType="{x:Type DataGridCell}" x:Key="DataGridStyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Row}" Value="1">
                <Setter Property="Foreground" Value="Red" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Row}" Value="2">
                <Setter Property="Foreground" Value="Orange" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Row}" Value="3">
                <Setter Property="Foreground" Value="Orange" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Row}" Value="4">
                <Setter Property="Foreground" Value="Green" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Row}" Value="5">
                <Setter Property="Foreground" Value="Green" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Row}" Value="6">
                <Setter Property="Foreground" Value="Green" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

然后,我使用这样的静态资源:

<DataGrid x:Name="DamageDealtData" HorizontalAlignment="Left" VerticalAlignment="Top" Width="506" Height="163" Canvas.Left="30" Canvas.Top="35">
                <DataGridCell Style="{StaticResource DataGridStyle}"/>
            </DataGrid>

这种方法也不起作用。因为它似乎只创建了1个DataGridCell并对其进行样式化。

我将如何做到这一点?

1 个答案:

答案 0 :(得分:2)

您的资源是正确的,您只是错误地应用它。

应该这样做:

<DataGrid x:Name="DamageDealtData" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="506" Height="163" Canvas.Left="30"
Canvas.Top="35" DataGrid.CellStyle="{StaticResource DataGridStyle}">
    ...
</DataGrid>

当您使用<DataGridCell Style="{StaticResource DataGridStyle}"/>时,您创建了一个DataGridCell并将其样式设置为您创建的样式。通过设置DataGrid.CellStyle,您可以将该样式应用于网格的所有单元格。