使用样式触发器增加UserControl大小? [英] Growing UserControl Size with Style Trigger?

查看:88
本文介绍了使用样式触发器增加UserControl大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 IsSelected DP设置为true时,我想使自定义UserControl由乘数增长。我当前的XAML如下所示:

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>

在上述示例中, MyBaseControl扩展了UserControl,并定义了IsSelected DP。

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

此代码目前无法正常运行,这是我的问题之一。另一个是我想将宽度/高度增加一定数量(例如:0.10),而不是将其设置为硬数字。这样,可以在源代码中定义控件时设置大小。

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.

感谢您的帮助!

添加代码:

MyBaseControl代码:

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代码:

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>


推荐答案

尝试一下

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

这篇关于使用样式触发器增加UserControl大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆