2010-03-01 63 views
1

我有一个自定义用户控件,它扩展了按钮控件,并添加了两个新的依赖项IsActive和Icon。在控件中有DataTrigger,它们根据该值设置Icon和Active状态。WPF DataTrigger没有射出ItemsControl外

我遇到的问题是仅在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); 
     } 
}  

任何帮助将不胜感激。

+0

它也适用于 <控件模板> <控件:FunctionButton IsActive = “真” 前景= “白” CONTENT = “家” 图标= “家” 命令=” {结合的HomeClick}” /> 看起来像蛋类问题之前鸡。 – cjibo 2010-03-01 19:50:44

回答

1

从包含的XAML中不完全清楚,因为您未显示声明x:Name =“MyFunctionButton”的位置或触发器的使用位置,但我假设您正在运行命名范围问题。您应该使用RelativeSource绑定(TemplatedParent或Self,取决于触发器的位置)或属性Trigger而不是DataTrigger,而不是使用ElementName。如果你可以在你的DataTrigger中包含更多的XAML来提供一些上下文,我可以给你一些更精确的答案。

+0

我应该抓住那个。哎呀。 – cjibo 2010-03-02 15:33:44