Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting RenderTransform in a nested style

I'm using .Net 3.5 on Windows 8, with the default Aero theme.

This was supposed to be an answer to Scale Checkbox without scaling content but it didn't work as easily as I expected. I'm trying to:

  1. scale the box in a checkbox control
  2. in a style, so I can apply the change to all checkboxes (or only some)
  3. independently of its text, with no need to compensate.

I have a UserControl with this Resources:

<UserControl.Resources>
    <ResourceDictionary>
        <!-- ... -->

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

Resources/CheckBoxStyle.xaml is this:

<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 primitives namespace is in case whatever I settle on needs to know what BulletDecorator is.

I found BulletDecorator in the Aero theme for .Net 3.5 from here, behind the "Default WPF Themes" link, per this answer.

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:

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:

<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>
like image 551
Grault Avatar asked Dec 15 '22 17:12

Grault


1 Answers

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:

<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>
like image 173
Grault Avatar answered Jan 01 '23 08:01

Grault