使用样式触发器增加UserControl大小?

时间:2012-07-19 19:49:14

标签: wpf user-controls triggers wpf-controls dependency-properties

当“IsSelected”DP设置为true时,我想使自定义UserControl按乘数增长。我目前的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”扩展了UserControl,并定义了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>

1 个答案:

答案 0 :(得分:3)

试试这个

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