Reputation: 1453
I have in all my datagrids some styles to be applied for some types but specifically inside the datagrid so it looks like this:
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="White"/>
<Setter Property="BorderThickness" Value="2"/>
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type vmcc:LOVComboBox}">
<EventSetter Event="Loaded" Handler="UIElement_GotFocus"></EventSetter>
</Style>
<Style TargetType="{x:Type TextBox}">
<EventSetter Event="Loaded" Handler="UIElement_GotFocus"></EventSetter>
</Style>
</DataGrid.Resources>
How can I create in a single point in my app (for example in a style file) the xaml code to do the same thing applying it to all the datagrids? I've done styles for textbox's for example, but this applies to elements inside the datagrid ...
Upvotes: 0
Views: 3271
Reputation: 450
Because you probably want to have event handlers in a style resource, I suggest to create ResourceDictionary with code behind. Just create UserControl in Visual Studio and replace the code inside - you will have both *.xaml and *.xaml.cs files nicely nested. In order to apply styles for DataGrid in whole application, include them in resources of the Application in the App.xaml:
<Application.Resources>
<ResourceDictionary Source="MyStyles.xaml"/>
</Application.Resources>
Code in the MyStyles.xaml file:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Example.MyStyles"
xmlns:vmcc="clr-namespace:Example">
<Style TargetType="{x:Type DataGridCell}" x:Key="DataGrid.DataGridCellStyle">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="White" />
<Setter Property="BorderThickness" Value="2" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type vmcc:LOVComboBox}" x:Key="DataGrid.LOVComboBoxStyle">
<EventSetter Event="Loaded" Handler="UIElement_GotFocus"></EventSetter>
</Style>
<Style TargetType="{x:Type TextBox}" x:Key="DataGrid.TextBoxStyle">
<EventSetter Event="Loaded" Handler="UIElement_GotFocus"></EventSetter>
</Style>
<Style TargetType="{x:Type DataGrid}">
<Style.Resources>
<Style TargetType="DataGridCell" BasedOn="{StaticResource DataGrid.DataGridCellStyle}" />
<Style TargetType="vmcc:LOVComboBox" BasedOn="{StaticResource DataGrid.LOVComboBoxStyle}" />
<Style TargetType="TextBox" BasedOn="{StaticResource DataGrid.TextBoxStyle}" />
</Style.Resources>
</Style>
</ResourceDictionary>
We want to have TextBox, LOVComboBox styles applied only inside the DataGrid - that's why styles for them are included in Style.Resources of DataGrid style. I tried to put there styles copied from your code (without x:Key attribute), but I got an error: The event 'PreviewMouseLeftButtonDown' cannot be specified on a Target tag in a Style. Use an EventSetter instead.. That's why I extracted them as resources with keys and used derived styles in Style.Resources of DataGrid style (as suggested here).
Code in the MyStyles.xaml.cs file:
public partial class MyStyles
{
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//your code here
}
private void UIElement_GotFocus(object sender, RoutedEventArgs e)
{
//your code here
}
}
Upvotes: 3