WPF 样式资源不起作用 [英] WPF Style Resource not Working

查看:40
本文介绍了WPF 样式资源不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

All, I want to inherit a generic style for all DataGrid/ResourceDataGrids in my application. To do this I have created a resource file called ResourceControl.xaml, in which I have

<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 中的哪个位置我有

Where in the resource file MainWindowResources.xaml I have

<!--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 中定义的样式,为什么?

But my ResourceDataGrid is not inheriting the style that is defined in the MainWindowResources.xaml, why?

推荐答案

所以,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>

输出:

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可以如下:

In this case, inheritance Style can be made as follows:

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

另外,也可以通过key继承:

Also, inheritance can be done by key:

<Style TargetType="{x:Type local:MyLabel}" BasedOn="{StaticResource MyStyle}">

这篇关于WPF 样式资源不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆