XAML 以嵌套样式设置RenderTransform

zqdjd7g9  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(69)

我在Windows 8上使用.NET 3.5,默认的Aero主题。
这应该是Scale Checkbox without scaling content的一个答案,但它并不像我预期的那样容易工作。我在努力:
1.缩放复选框控件中的框
1.这样我就可以将更改应用到所有CheckBox(或仅部分CheckBox)
1.独立于其文本,无需补偿。
我有一个UserControl,其中包含以下资源:

<UserControl.Resources>
    <ResourceDictionary>
        <!-- ... -->
        
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../Resources/CheckBoxStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

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

primitives命名空间是为了以防我需要知道BulletDecorator是什么。

我在here的.Net 3.5的Aero主题中发现了BulletDecorator,在“Default WPF Themes”链接后面,每个this answer
我没有看到我的复选框大小的差异。我不认为我从主题中抓取了错误的元素类型,但还能发生什么呢?

编辑1:

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

xxslljrj1#

我找到了一个解决方案,虽然它并不完美。一个更好的答案仍然会被接受。
我使用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>

相关问题