将样式资源分配给DataGrid [英] Assigning Style resources to a DataGrid

查看:110
本文介绍了将样式资源分配给DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为这个问题的朦胧性质而道歉,但是我对WPF还是陌生的,因此在资源问题上苦苦挣扎.

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.

我的问题是我有一个DataGrid,我想为其分配一种样式来描述诸如FontSizeBackground/Foreground颜色的属性(当鼠标悬停在行上时).我可以成功完成以下操作:

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

<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>

但是我知道/希望必须有一种方法可以将此RowStyle定义为单独的资源,然后从DataGrid定义本身中引用该资源(通过名称).因此,我尝试了创建Window.Resources标记和在引用它的DataGrid内的标记.

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

请参见以下内容:

<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.

关于, 大卫.

推荐答案

仔细查看您的代码... :)以便完整起见

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}" ...>

这篇关于将样式资源分配给DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆