1

我有一个自定义用户控件,它扩展了按钮控件并添加了两个新的依赖属性 IsActive 和 Icon。在控件中有根据值设置图标和活动状态的 DataTriggers。

我遇到的问题是该控件仅在 ItemControl 内部工作。这是项目控件内外的控件的 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);
        }
 }    

任何帮助将非常感激。

4

1 回答 1

1

从包含的 XAML 中并不清楚,因为您没有显示 x:Name="MyFunctionButton" 的声明位置或触发器的使用位置,但我假设您遇到了名称范围问题。您应该使用 RelativeSource 绑定(TemplatedParent 或 Self 取决于触发器的位置)或属性 Trigger 而不是 DataTrigger,而不是使用 ElementName。如果您可以在 DataTrigger 周围包含更多 XAML 以提供一些上下文,我可以通过一些代码为您提供更准确的答案。

于 2010-03-02T00:08:26.887 回答