在资源字典中使用 BasedOn 设置 Elements.Resource 样式

发布于 2024-10-07 16:56:18 字数 3797 浏览 5 评论 0原文

我有一个资源字典,用于定义应用程序的外观(样式)。

我刚刚创建了另一个资源字典,其中包含我在多个不同屏幕(甚至在同一屏幕内多次)使用的数据模板来显示我的业务对象。

我想更改 DataTemplates 中的一些默认样式,以便控件更适合;但是我希望控件继承与屏幕其余部分相同的样式。因此,我自然想使用 BasedOn 属性来完成此任务。

我遇到的问题是我不确定将 BasedOn 属性设置为什么。

例如,在包含我的样式(称为“myStyle.xaml”)的资源字典中,我有:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <Style TargetType="{x:Type Label}">
        <Setter Property="Foreground" Value="#F5F5F5" />
        <Setter Property="FontSize" Value="12"></Setter>
        <Setter Property="Width" Value="120"></Setter>
        <Setter Property="FontFamily" Value="Arial"></Setter>
  </Style>
  <Style TargetType="{x:Type TextBox}">
        <Setter Property="FontSize" Value="12"></Setter>
        <Setter Property="Width" Value="120"></Setter>
        <Setter Property="Height" Value="25"></Setter>
        <Setter Property="Background" Value="Black"></Setter>
  </Style>
  <!-- .... and so on .... -->
</ResourceDictionary>

我在以下窗口中使用此资源:

<Window x:Class="SiteSetupWindow4"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
        Title="A Screen">

    <Window.Resources>
        <ResourceDictionary x:Key="defaultStyleX">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary x:Name="DefaultStyles" Source="Resources/myStyle.xaml" />
                <ResourceDictionary  x:Name="Templates" Source="Resources/myTemplates.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </Window.Resources>

现在,我有另一个资源字典,其中包含我在窗口中使用的 DataTemplates。它被称为“我的模板”。该样式按预期应用于 DataTemplate;但是,我想覆盖 DataTemplate 中样式的某些方面(例如宽度)。

这就是我已经厌倦的,但是我无法让 BasedOn 属性工作...

(myTemplate.xaml)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

   <DataTemplate x:Key="PanelInfo">
            <StackPanel>
                <StackPanel.Resources>
                    <Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
                        <Setter Property="Width" Value="120" />
                    </Style>
                    <Style TargetType="Label">
                        <Setter Property="Width" Value="180" />
                    </Style>
                    <Style TargetType="ComboBox">
                        <Setter Property="Width" Value="120" />
                    </Style>
                <StackPanel.Resources>
                <StackPanel Orientation="Horizontal">
                    <Label Content="Type:"></Label>
                    <ComboBox>
                        <ComboBoxItem Content="{Binding Path=Type}" IsSelected="True"></ComboBoxItem>
                    </ComboBox>
                    <!--...and so on -->
                </StackPanel>
            </StackPanel>
</ResourceDictionary>

这失败了....我也尝试过使用 DynamicResource,但这也失败了。 我不知道如何解决这个问题。

任何建议将不胜感激!

谢谢,

-弗林尼

I have a Resource Dictionary that I am using to define the look-and-feel (style) for my application.

I have just created another Resource Dictionary that contains DataTemplates that I am using on several different screens (and even multiple times within the same screen) to display my business objects.

I would like to change some of the default styles within my DataTemplates so that the controls fit better; however I would like the controls to inherit the same style as the rest of the screen. So, naturally I want to use the BasedOn property for this task.

The problem that I am having is that I'm not sure what to set the BasedOn property to.

For example, in the resource dictionary that contains my styles (called "myStyle.xaml") I have:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <Style TargetType="{x:Type Label}">
        <Setter Property="Foreground" Value="#F5F5F5" />
        <Setter Property="FontSize" Value="12"></Setter>
        <Setter Property="Width" Value="120"></Setter>
        <Setter Property="FontFamily" Value="Arial"></Setter>
  </Style>
  <Style TargetType="{x:Type TextBox}">
        <Setter Property="FontSize" Value="12"></Setter>
        <Setter Property="Width" Value="120"></Setter>
        <Setter Property="Height" Value="25"></Setter>
        <Setter Property="Background" Value="Black"></Setter>
  </Style>
  <!-- .... and so on .... -->
</ResourceDictionary>

I am using this resource in the following window:

<Window x:Class="SiteSetupWindow4"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:primatives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
        Title="A Screen">

    <Window.Resources>
        <ResourceDictionary x:Key="defaultStyleX">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary x:Name="DefaultStyles" Source="Resources/myStyle.xaml" />
                <ResourceDictionary  x:Name="Templates" Source="Resources/myTemplates.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </Window.Resources>

Now, I have another Resource Dictionary that contains DataTemplates that I am using within my window. It is called "myTemplates". The style is applied to the DataTemplate as expected; however, I would like to overwrite some aspects of the style within the DataTemplate (Like width for example).

This is what I have tired, however I cannot get the BasedOn property to work...

(myTemplate.xaml)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

   <DataTemplate x:Key="PanelInfo">
            <StackPanel>
                <StackPanel.Resources>
                    <Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
                        <Setter Property="Width" Value="120" />
                    </Style>
                    <Style TargetType="Label">
                        <Setter Property="Width" Value="180" />
                    </Style>
                    <Style TargetType="ComboBox">
                        <Setter Property="Width" Value="120" />
                    </Style>
                <StackPanel.Resources>
                <StackPanel Orientation="Horizontal">
                    <Label Content="Type:"></Label>
                    <ComboBox>
                        <ComboBoxItem Content="{Binding Path=Type}" IsSelected="True"></ComboBoxItem>
                    </ComboBox>
                    <!--...and so on -->
                </StackPanel>
            </StackPanel>
</ResourceDictionary>

This fails....I have also tried using DynamicResource, but this also fails.
I'm not sure how to get around this.

Any advise would be greatly appreciated!

Thanks,

-Frinny

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别挽留 2024-10-14 16:56:18

我在扩展按钮样式时遇到了同样的问题。

ResourceKey= 为我解决了这个问题。

这有效:

<Style x:Name="ButtonVisibility" 
       TargetType="{x:Type Button}" 
       BasedOn="{StaticResource ResourceKey={x:Type Button}}">

I was having the same problem with an extended Button Style.

The ResourceKey= is what solved it for me.

This worked:

<Style x:Name="ButtonVisibility" 
       TargetType="{x:Type Button}" 
       BasedOn="{StaticResource ResourceKey={x:Type Button}}">
尽揽少女心 2024-10-14 16:56:18

您对类型的 BasedOn 方式是正确的。理论上,只要在运行时,您所基于的样式正确地合并到树中,这就会起作用。确保您已正确合并“myStyles.xaml”。您可以通过删除尝试修改的样式来检查这一点,并确保它在“myStyles.xaml”中的样式中正确显示。

如果不是,那么有很多地方可能会出错,但尝试合并正在处理的文件中的样式总是有帮助的,然后在树上查找缺失的位置。

该实用程序将帮助您查看运行时树中发生的情况。

http://blois.us/Snoop/

The way you have BasedOn for a type is correct. This will work in theory as long as, at run time, the style that you are basing it on is merged into the tree correctly. Make sure you have the "myStyles.xaml" merged in correctly. You can check this by removing your style you tried to modify and make sure it displays correctly from your style in "myStyles.xaml."

If it isn't there are a lot of places you can go wrong, but it always helps to try merging the styles in the file you are working on, then work up the tree to see where it's missing.

This utility will help you look at what is happing in the tree at run time.

http://blois.us/Snoop/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文