Vaccano
Vaccano

Reputation: 82377

Make my Style specific to a single instance

My wpf application has this Style:

Xaml:

<Style x:Key="customTableViewDataRowStyle" TargetType="{x:Type xcdg:DataRow}">

C#

this.Resources.Add(typeof(DataRow), this.FindResource("customTableViewDataRowStyle"));

It modifies the look of a Row in an Xceed Datagrid.

This all works great!

But I went and added another Xceed Datagrid to my app and it is using the style too.

Is there a way to not have it do that? Can I make it only affect specific grids?

Upvotes: 1

Views: 143

Answers (1)

menty
menty

Reputation: 416

You could seperate the style into a standalone ResourceDictionary and then reference the ResourceDictionary only in the desired DataGrids.

Example with two DataGrids an only one of them have the style set:

CustomDataGridStyles.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type DataGrid}">
        <Setter Property="Background" Value="Red" />
        <!-- Other Style Settings -->
    </Style>

</ResourceDictionary>

Window:

<Window x:Class="SpecificControlStyle.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>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <DataGrid Grid.Row="0">
            <DataGrid.Resources>
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="CustomDataGridStyles.xaml" />
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
            </DataGrid.Resources>
        </DataGrid>
        <DataGrid Grid.Row="1" />
    </Grid>
</Window>

Upvotes: 1

Related Questions