1

ボタン コントロールを拡張し、2 つの新しい依存プロパティ IsActive と Icon を追加するカスタム UserControl があります。コントロールには、値に基づいて Icon と Active 状態を設定する DataTrigger があります。

私が抱えている問題は、コントロールが 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);
        }
 }    

どんな助けでも大歓迎です。

4

1 に答える 1

1

x:Name="MyFunctionButton" が宣言されている場所やトリガーが使用されている場所を示していないため、含まれている XAML からは正確にはわかりませんが、名前のスコープの問題が発生していると思います。ElementName を使用する代わりに、RelativeSource バインディング (トリガーの場所に応じて TemplatedParent または Self) または DataTrigger の代わりにプロパティ Trigger を使用する必要があります。コンテキストを提供するために DataTrigger の周りに XAML をさらに含めることができれば、いくつかのコードでより正確な回答を提供できます。

于 2010-03-02T00:08:26.887 に答える