Growing UserControl Size with Style Trigger?

允我心安 提交于 2019-12-11 03:49:28

问题


I would like to cause a custom UserControl to grow by a multiplier when an "IsSelected" DP is set to true. My current XAML looks like this:

<ctrl:MyBaseControl x:Class="MyDemo.Controls.MyCustomControl"
         ...>
<ctrl:MyBaseControl.Resources>
    <Style TargetType="{x:Type ctrl:MyCustomControl}">
        <Setter Property="BorderBrush" Value="White" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="BorderThickness" Value="2" />
                <Setter Property="Width" Value="340" />
                <Setter Property="Height" Value="260" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ctrl:MyBaseControl.Resources>
<Border>
    <StackPanel>
        ...
    </StackPanel>
</Border>

In the above sample, "MyBaseControl" extends UserControl, and defines the IsSelected DP.

This code just plain isn't working at the moment, which is one of my issues. The other is that I would like to grow the Width/Height for a certain amount (for example: 0.10) instead of setting it to a hard number. This way I can set the size when I define the control at the source.

Thanks for any help!

ADDITION CODE:

MyBaseControl Code:

public abstract class MyBaseControl: UserControl
{
    public static readonly DependencyProperty IsSelectedProperty =
        DependencyProperty.Register(
        "IsSelected",
        typeof(Boolean),
        typeof(MyBaseControl),
        new PropertyMetadata(null));

    public MyBaseControl() : base() { }

    #region Properties

    public Boolean IsSelected
    {
        get { return (Boolean)GetValue(IsSelectedProperty); }
        set { SetValue(IsSelectedProperty, value); }
    }

    #endregion Properties

}

MyCustomControl Code:

public partial class MyCustomControl: MyBaseControl
{
    public static readonly DependencyProperty IconProperty =
        DependencyProperty.Register(
        "Icon",
        typeof(ImageSource),
        typeof(MyCustomControl),
        new PropertyMetadata(null));

    public static readonly DependencyProperty BlurbProperty =
        DependencyProperty.Register(
        "Blurb",
        typeof(String),
        typeof(MyCustomControl),
        new PropertyMetadata(null));

    public MyCustomControl()
    {
        InitializeComponent();
    }

    #region Properties

    public ImageSource Icon
    {
        get { return (ImageSource)GetValue(IconProperty); }
        set { SetValue(IconProperty, value); }
    }

    public String Blurb
    {
        get { return (String)GetValue(BlurbProperty); }
        set { SetValue(BlurbProperty, value); }
    }

    #endregion Properties
}

Example of working trigger on internal elements:

    <Style TargetType="{x:Type Border}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ctrl:MyCustomControl}}, Path=IsSelected}" Value="True">
                <Setter Property="BorderThickness" Value="5" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

回答1:


Try this

<ctrl:MyBaseControl.Resources>
    <Style TargetType="{x:Type ctrl:MyCustomControl}">
        <Setter Property="BorderBrush" Value="White" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="BorderThickness" Value="2" />
                <Setter Property="RenderTransform" >
                   <Setter.Value>
                       <ScaleTransform ScaleX="1.1" ScaleY="1.1" />
                   </Setter.Value>
                </Setter>
                <Setter Property="RenderTransformOrigin" Value="0.5, 0.5"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ctrl:MyBaseControl.Resources>


来源:https://stackoverflow.com/questions/11568214/growing-usercontrol-size-with-style-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!