我有一个自定义UserControl,它扩展了button控件并添加了两个新的依赖属性IsActive和Icon。在控件中,有DataTriggers根据值设置图标和活动状态。
我遇到的问题是控件只在ItemControl内部工作。下面是Items控件内外的控件的XAML部分。当控件不在项控件中时,触发器不工作。
<ItemsControl ItemsSource="{Binding HomeList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Controls:FunctionButton IsActive="True" Foreground="White" Content="Home" Icon="Home" Command="{Binding HomeClick}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Controls:FunctionButton IsActive="True" Foreground="White" Content="Home" Icon="Home" Command="{Binding HomeClick}" />
这是控件中的DataTrigger。
<DataTrigger Binding="{Binding ElementName=MyFunctionButton, Path=Icon}" Value="Home">
<Setter TargetName="HomeIcon" Property="Visibility" Value="Visible" />
</DataTrigger>
唯一不起作用的属性是我的依赖项属性。
public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register("IsActive", typeof(bool), typeof(FunctionButton));
public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof (Icon), typeof (FunctionButton));
/// <summary>
/// Gets or sets a value indicating whether this instance is active.
/// </summary>
/// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value>
public bool IsActive
{
get
{
return (bool) base.GetValue(IsActiveProperty);
}
set
{
base.SetValue(IsActiveProperty, value);
}
}
/// <summary>
/// Gets or sets the icon.
/// </summary>
/// <value>The icon.</value>
public Icon Icon
{
get
{
return (Icon) base.GetValue(IconProperty);
}
set
{
base.SetValue(IconProperty, value);
}
}
任何帮助都将不胜感激。