0

我的 wpf 应用程序有这种风格:

xml:

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

C#

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

它修改 Xceed Datagrid 中行的外观。

这一切都很好!

但是我去我的应用程序中添加了另一个 Xceed Datagrid,它也使用了这种样式。

有没有办法让它不这样做?我可以让它只影响特定的网格吗?

4

1 回答 1

1

您可以将样式分离为独立的 ResourceDictionary,然后仅在所需的 DataGrids 中引用 ResourceDictionary。

具有两个 DataGrid 的示例,其中只有一个具有样式集:

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 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>
于 2012-10-30T07:58:29.837 回答