wpf 将样式资源分配给 DataGrid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22526292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:16:43  来源:igfitidea点击:

Assigning Style resources to a DataGrid

wpfxamldatagridresourcesstaticresource

提问by David Coorey

Apologies for the maybe hazy nature of this question, but I am fairly new to WPF and am consequently struggling with the issue of resources.

为这个问题的模糊性质道歉,但我对 WPF 相当陌生,因此我正在努力解决资源问题。

My problem is that I have a DataGridthat I wish to assign a style to that describes properties such as the FontSizeand Background/Foregroundcolours (when the mouse hovers over the rows). I can do this successfully as follows:

我的问题是我有一个DataGrid我希望分配一个样式来描述FontSizeBackground/Foreground颜色等属性(当鼠标悬停在行上时)。我可以成功地做到这一点如下:

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <DataGrid Name="DataGrid1" ItemsSource="{Binding Path=Fibers}">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="FontSize" Value="12"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Blue"/>
                            <Setter Property="Foreground" Value="White"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>

            <DataGrid.Columns>
                <DataGridTextColumn Header="FiberNo" />
                <DataGridTextColumn Header="Fiber" />
                <DataGridTextColumn Header="Connection" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

But I know/hope that there must be a way to define this RowStyleas a separate resource, and then refer to this resource (via a name) from within the DataGriddefinition itself. I therefore have tried creating a Window.Resourcestag and a tag within the DataGridthat refers to it.

但我知道/希望必须有一种方法将它定义RowStyle为一个单独的资源,然后从DataGrid定义本身中引用这个资源(通过名称)。因此,我尝试Window.ResourcesDataGrid引用它的 中创建一个标签和一个标签。

Please see below:

请参阅以下内容:

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <DataGrid x:Key="MyDataGridStyle">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="FontSize" Value="12"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Blue"/>
                            <Setter Property="Foreground" Value="White"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    </Window.Resources>

    <Grid>
        <DataGrid Name="DataGrid1" ItemsSource="{Binding Path=Fibers}">
            <StaticResource ResourceKey="MyDataGridStyle"/>

            <DataGrid.Columns>
                <DataGridTextColumn Header="FiberNo" />
                <DataGridTextColumn Header="Fiber" />
                <DataGridTextColumn Header="Connection" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Needless to say that this does not work. It doesn't crash, but I am not seeing any rows, either. The code I have supplied here is a scaled-down version of what I have written for my application, but the essentials are the same.

不用说,这行不通。它不会崩溃,但我也没有看到任何行。我在此处提供的代码是我为应用程序编写的代码的缩小版本,但基本要素是相同的。

Regards, David.

问候,大卫。

回答by blindmeis

EDIT: did not look carefully on your code... :) so for completeness

编辑:没有仔细查看您的代码... :) 所以为了完整性

<Window.Resources>
            <Style x:Key="MyDataGridStyle" TargetType="DataGridRow">
                <Setter Property="FontSize" Value="12"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Blue"/>
                        <Setter Property="Foreground" Value="White"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
</Window.Resources>

<DataGrid Name="DataGrid1"
          ItemsSource="{Binding Path=Fibers}" 
          RowStyle="{StaticResource MyDataGridStyle}" ...>