wpf 在嵌套样式中设置 RenderTransform
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27629285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting RenderTransform in a nested style
提问by Grault
I'm using .Net 3.5 on Windows 8, with the default Aero theme.
我在 Windows 8 上使用 .Net 3.5,默认 Aero 主题。
This was supposed to be an answer to Scale Checkbox without scaling contentbut it didn't work as easily as I expected. I'm trying to:
这应该是不缩放内容的 Scale Checkbox的答案,但它并没有像我预期的那样容易。我试图:
- scale the box in a checkbox control
- in a style, so I can apply the change to all checkboxes (or only some)
- independently of its text, with no need to compensate.
- 缩放复选框控件中的框
- 以一种样式,因此我可以将更改应用于所有复选框(或仅某些)
- 独立于其文本,无需补偿。
I have a UserControlwith this Resources:
我有一个带有此资源的UserControl:
<UserControl.Resources>
<ResourceDictionary>
<!-- ... -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/CheckBoxStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Resources/CheckBoxStyle.xamlis this:
资源/CheckBoxStyle.xaml是这样的:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework">
<Style TargetType="{x:Type CheckBox}">
<Style.Resources>
<Style TargetType="{x:Type BulletDecorator}">
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform ScaleX="2" ScaleY="2"/>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
</Style>
</ResourceDictionary>
The primitivesnamespaceis in case whatever I settle on needs to know what BulletDecorator is.
该primitives命名空间是什么我看中需要知道BulletDecorator是什么情况。
I found BulletDecoratorin the Aero theme for .Net 3.5 from here, behind the "Default WPF Themes" link, per this answer.
我从这里找到了 .Net 3.5 的 Aero 主题中的BulletDecorator,在“默认 WPF 主题”链接后面,根据这个答案。
I'm seeing no difference in the size of my checkbox boxes. I don't think I grabbed the wrong element type from the theme, but what else could be happening?
我发现复选框的大小没有区别。我不认为我从主题中获取了错误的元素类型,但还会发生什么?
Edit 1:
编辑1:
BulletDecorator contains the checkbox's content anyway, so I tried dropping requirement #3. Now I have a huge box and huge text, and want to shrink the text back down. Here is CheckBoxStyle.xaml:
BulletDecorator 无论如何都包含复选框的内容,所以我尝试删除要求 #3。现在我有一个巨大的盒子和巨大的文本,并且想要缩小文本。这是CheckBoxStyle.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework">
<Style TargetType="{x:Type CheckBox}">
<Style.Resources>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<ContentPresenter>
<ContentPresenter.LayoutTransform>
<ScaleTransform ScaleX="0.5" ScaleY="0.5" />
</ContentPresenter.LayoutTransform>
</ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
<Setter Property="LayoutTransform">
<Setter.Value>
<ScaleTransform ScaleX="2" ScaleY="2"/>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
回答by Grault
I found a solution, although it's not perfect. A better answer would still be accepted.
我找到了一个解决方案,虽然它并不完美。更好的答案仍然会被接受。
I used Snoopto determine that the element I'm concerned with is already a ContentPresenter. It contains a TextBlock, but for some reason that can't be styled (and it's shown in parentheses in Snoop's hierarchy). Here's my resulting CheckBoxStyle.xaml:
我使用Snoop来确定我关心的元素已经是一个 ContentPresenter。它包含一个 TextBlock,但由于某种原因无法设置样式(并且它显示在 Snoop 层次结构的括号中)。这是我生成的CheckBoxStyle.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework">
<Style TargetType="{x:Type CheckBox}">
<Style.Resources>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="LayoutTransform">
<Setter.Value>
<ScaleTransform ScaleX="0.5" ScaleY="0.5" />
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
<Setter Property="LayoutTransform">
<Setter.Value>
<ScaleTransform ScaleX="2" ScaleY="2"/>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

