2

所有,我想为DataGrid/ResourceDataGrid我的应用程序中的所有 s 继承一个通用样式。为此,我创建了一个名为 的资源文件ResourceControl.xaml,其中我有

<UserControl x:Class="ResourceStudio.Views.ResourceControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:viewModels="clr-namespace:ResourceStudio.ViewModels" 
             xmlns:dataAccess="clr-namespace:ResourceStudio.DataAccess" 
             xmlns:controls="clr-namespace:ResourceStudio.Controls"
             mc:Ignorable="d">
   <UserControl.Resources>
      <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MainWindowResources.xaml" />
         </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
   </UserControl.Resources>
   <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
      <TextBox DockPanel.Dock="Top" 
               Name="searchBox" 
               Margin="0,2" 
               VerticalContentAlignment="Center"
               mahAppsControls:TextboxHelper.Watermark="Search Resources" 
               mahAppsControls:TextboxHelper.ClearTextButton="True">
      </TextBox>
      <Grid DockPanel.Dock="Top">
         <controls:ResourceDataGrid x:Name="resourceDataGrid" 
                                    ItemsSource="{Binding Path=Resources}" 
                                    dataAccess:DataGridTextSearch.SearchValue="{Binding ElementName=searchBox, Path=Text, UpdateSourceTrigger=PropertyChanged}" 
                                    dataAccess:DataGridTextSearch.IsAnyTextMatch="False"
                                    HorizontalAlignment="Stretch" 
                                    VerticalAlignment="Stretch" 
                                    AutoGenerateColumns="False" 
                                    GridLinesVisibility="None"
                                    RowHeaderWidth="0" 
                                    CanUserAddRows="True" 
                                    CanUserDeleteRows="True">
            <controls:ResourceDataGrid.Columns>
               <DataGridTextColumn Header="KeyIndex" Binding="{Binding KeyIndex}" IsReadOnly="True"/>
               <DataGridTextColumn Header="FileName" Binding="{Binding FileName}" IsReadOnly="True"/>
               <DataGridTextColumn Header="ResourceName" Binding="{Binding ResourceName}" IsReadOnly="False"/>
               <controls:CollectionTextColumn Collection="ResourceStringList" Visibility="Collapsed"/>
            </controls:ResourceDataGrid.Columns>
            <controls:ResourceDataGrid.Resources>
               <dataAccess:SearchValueConverter x:Key="searchValueConverter"/>
               <Style TargetType="{x:Type DataGridCell}">
                  <Setter Property="dataAccess:DataGridTextSearch.IsTextMatch">
                     <Setter.Value>
                        <MultiBinding Converter="{StaticResource searchValueConverter}">
                           <Binding RelativeSource="{RelativeSource Self}" Path="Content.Text" />
                           <Binding RelativeSource="{RelativeSource Self}" Path="(dataAccess:DataGridTextSearch.SearchValue)" />
                           <Binding ElementName="resourceDataGrid" />
                        </MultiBinding>
                     </Setter.Value>
                  </Setter>
                  <Style.Triggers>
                     <Trigger Property="dataAccess:DataGridTextSearch.IsTextMatch" Value="True">
                        <Setter Property="Background" Value="Orange" />
                     </Trigger>
                  </Style.Triggers>
               </Style>
            </controls:ResourceDataGrid.Resources>
         </controls:ResourceDataGrid>
      </Grid>
   </DockPanel>
</UserControl>

MainWindowResources.xaml我在资源文件的哪个位置

<!--DataGrid Style-->
<Style TargetType="{x:Type DataGrid}">
   <Setter Property="Background" Value="White"/>
   <Style.Triggers>
      <Trigger Property="IsSelected" Value="True">
         <!--<Setter Property="Background" Value="{DynamicResource AccentColor}"/>-->
         <Setter Property="Background" Value="Red"/>
         <Setter Property="Foreground" Value="White"/>
      </Trigger>
   </Style.Triggers>
</Style>

<!--ResourceDataGrid Style-->
<Style TargetType="{x:Type controls:ResourceDataGrid}">
   <Setter Property="Background" Value="White"/>
   <Style.Triggers>
      <Trigger Property="IsSelected" Value="True">
         <!--<Setter Property="Background" Value="{DynamicResource AccentColor}"/>-->
         <Setter Property="Background" Value="Red"/>
         <Setter Property="Foreground" Value="White"/>
      </Trigger>
   </Style.Triggers>
</Style>

但是我ResourceDataGrid没有继承中定义的样式MainWindowResources.xaml,为什么?

4

2 回答 2

1

试试这个:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/{YourAssemblyWhereResourceDictionaryIsLocated};component/Resources/MainWindowResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

component/Resources/MainWindowResources.xaml是样式表的确切位置

于 2013-07-27T13:56:19.677 回答
1

所以, 的例子Label

清单UserControl

<UserControl x:Class="ResourceDictionaryHelp.ResourceControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:ResourceDictionaryHelp"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MainWindowResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

<Grid>
    <Label Content="TestLabel" HorizontalAlignment="Left" />

    <local:MyLabel Content="TestMyLabel" HorizontalAlignment="Right" />
</Grid>

</UserControl>

后面的代码UserControl

public partial class ResourceControl : UserControl
{
    public ResourceControl()
    {
        InitializeComponent();
    }
}

public class MyLabel : Label 
{
    public MyLabel() 
    {
        base.OnApplyTemplate();
    }   
}

使用UserControl

<Window x:Class="ResourceDictionaryHelp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ResourceDictionaryHelp"
    WindowStartupLocation="CenterScreen"
    Title="MainWindow" Height="350" Width="525">

<Grid>
    <local:ResourceControl Width="300" Height="300" />
</Grid>

</Window>

Output

基于示例

清单ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:ResourceDictionaryHelp">

<Style TargetType="{x:Type Label}">
    <Setter Property="Background" Value="Green" />
    <Setter Property="Width" Value="100" />
    <Setter Property="Height" Value="100" />
</Style>

<Style TargetType="{x:Type local:MyLabel}">
    <Setter Property="Background" Value="Pink" />
    <Setter Property="Width" Value="100" />
    <Setter Property="Height" Value="100" />
</Style>

</ResourceDictionary>

Style在这种情况下,可以按如下方式进行继承:

<Style TargetType="{x:Type local:MyLabel}" BasedOn="{StaticResource {x:Type Label}}">
    <Setter Property="Background" Value="Pink" />
</Style>

它将与以下内容相同:

<Style TargetType="{x:Type local:MyLabel}">
    <Setter Property="Background" Value="Pink" />
    <Setter Property="Width" Value="100" />
    <Setter Property="Height" Value="100" />
</Style>

此外,继承可以通过键来完成:

<Style TargetType="{x:Type local:MyLabel}" BasedOn="{StaticResource MyStyle}">
于 2013-07-28T15:53:40.393 回答