2013-07-27 66 views
2

所有,我想在我的应用程序中继承所有DataGrid/ResourceDataGrid s的通用样式。要做到这一点,我创建了一个称为ResourceControl.xaml的资源文件,在其中我有WPF风格资源不工作

<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,为什么?

+0

MainWindowResources.xaml和ResourceControl是否在同一个文件夹中?我认为你必须设置你的ResourceDictionary的确切路径。 – Nitesh

+0

即使您的DataGrid继承了MainWindowResources.xaml中定义的样式,您是否检查过? – Nitesh

+0

是的,他们是在同一个目录下,它应该默认继承,因为资源被定义在视觉树的上方... – MoonKnight

回答

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

试试这个:

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

component/Resources/MainWindowResources.xaml是你的样式表