WPF样式资源不起作用

时间:2013-07-27 13:04:27

标签: c# wpf resources styles

全部,我想继承我的应用程序中所有DataGrid/ResourceDataGrid的通用样式。为此,我创建了一个名为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中定义的样式,为什么?

2 个答案:

答案 0 :(得分:1)

尝试一下:

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

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

答案 1 :(得分: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

BasedOn example

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}">