Assigning Style resources to a DataGrid

徘徊边缘 提交于 2020-01-24 07:46:19

问题


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.

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>

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.

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.


回答1:


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


来源:https://stackoverflow.com/questions/22526292/assigning-style-resources-to-a-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!