成长的用户控件的尺寸与样式触发?(Growing UserControl Size with Sty

2019-09-19 20:13发布

我想原因当“IsSelected” DP设置为true的自定义用户控件由一个乘数增长。 我现在的XAML看起来像这样:

<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”延伸用户控件,并定义IsSelected DP。

该代码只是简单的没有在时刻,这是我的问题,一个人工作。 另一种是,我想增长的宽度/高度为一定量(例如:0.10)代替其设置为硬一些。 这样我可以设置大小,当我确定在源头控制。

谢谢你的帮助!

附加码:

MyBaseControl代码:

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

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
}

工作内部元件触发的实施例:

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

Answer 1:

尝试这个

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


文章来源: Growing UserControl Size with Style Trigger?