【问题标题】:Growing UserControl Size with Style Trigger?使用样式触发器增加 UserControl 大小?
【发布时间】:2012-07-19 19:49:13
【问题描述】:

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

【问题讨论】:

  • 您能否发布您的 UserControl 的代码,以及操作 IsSelected DP 的代码?
  • 嗨 KDiTraglia,我已经用其他代码更新了我的帖子。
  • 有什么东西会改变 IsSelected 的状态吗?从发布的代码来看,它似乎总是错误的(除非你没有添加会改变它的部分)
  • 我没有包含代码,但它正在设置中(我通过将自定义光标移动到控件上来实现)。我在末尾添加了另一段有效的代码——所以我知道 IsSelected 正在通过。我在这个工作块上尝试过类似的变体来控制 MyCustomControl 本身,但没有运气。

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


【解决方案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>

【讨论】:

  • 我正在尝试一些变体,但到目前为止触发器没有触发。我将 - Property="IsSelected" - 更新为 - Binding="{Binding IsSelected}",但仍然没有运气。无论出于何种原因,事物都不想对控件本身采取行动。
  • 知道了。我将“DataTrigger”更新为常规的“Trigger”。我不确定我现在拥有的内容与我最初发布的内容之间有什么区别,但它至少可以正常工作。 :) 附带问题 - 使用变换,我怎样才能让它缩放并保持中心点(而不是左上角)?
  • 找到它: -- 再次感谢 KDiTraglia。
  • @EvilClosetMonkey 是的,我自己也想通了,很高兴能帮上忙(虽然我觉得大部分繁重的工作都是你自己完成的 =))
猜你喜欢
  • 2015-10-14
  • 2019-11-28
  • 2023-04-08
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
相关资源
最近更新 更多