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