【发布时间】: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