簡體   English   中英

在WPF中的ListBoxItem上觸發觸發器時,如何設置父元素的屬性

[英]How to set a property of the parent element, when firing a trigger on ListBoxItem in WPF

我有一個ResourceDictionary ,它的Style負責創建垂直側邊菜單的樣式。 下面是一個XAML ResourceDictionary的一個假設示例,用於演示該問題:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:control="clr-namespace:FirstFloor.ModernUI.Presentation">
<Style TargetType="control:Controle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="control:Controle">
                <Grid>
                    <Border Background="{TemplateBinding BackColor}">
                        <ListBox x:Name="LinkList" ItemsSource="{Binding Links,RelativeSource={RelativeSource TemplatedParent}}" Background="Transparent">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Grid Height="50"  Width="500"  >
                                        <TextBlock Text="{Binding DisplayName}" Foreground="Black" Margin="45,2,2,2" FontSize="{DynamicResource MediumFontSize}" TextTrimming="CharacterEllipsis" VerticalAlignment="Center" HorizontalAlignment="Left" />
                                    </Grid>
                                    <DataTemplate.Triggers>
                                        <Trigger Property="IsMouseOver" Value="true">
                                            <Trigger.Setters>
                                                <Setter Property="control:Controle.BackColor" Value="Red"/>
                                            </Trigger.Setters>
                                        </Trigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                  </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

該樣式基於從Control繼承的名為Controle的類。 以下是此類的代碼:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FirstFloor.ModernUI.Presentation
{
public class Controle:Control
{
    /// <summary>
    /// Identifies the Links dependency property.
    /// </summary>
    public static readonly DependencyProperty LinksProperty = DependencyProperty.Register("Links", typeof(LinkCollection), typeof(Controle), new PropertyMetadata(new LinkCollection()));

    /// <summary>
    /// Identifies the Links dependency property.
    /// </summary>
    public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register("BackColor", typeof(SolidColorBrush), typeof(Controle), new PropertyMetadata( new SolidColorBrush(Color.FromRgb(0, 200,0))));
    /// <summary>
    /// Gets or sets the collection of links that define the available content in this tab.
    /// </summary>
    /// 
    public LinkCollection Links
    {
        get { return (LinkCollection)GetValue(LinksProperty); }
        set { SetValue(LinksProperty, value); }
    }

    /// <summary>
    /// Gets or sets the collection of links that define the available content in this tab.
    /// </summary>
    public SolidColorBrush BackColor
    {
        get { return (SolidColorBrush)GetValue(BackColorProperty); }
        set { SetValue(BackColorProperty, value); }
    }
}
}

我想知道為什么在下面的行中:

 <Setter Property="control:Controle.BackColor" Value="Red"/>

我無法設置Controle的屬性...有趣的是,如果我設置了其他任何地方的禁令所有權,似乎都發生了,但是當我進入ItemTemplate並通過設置它無效時。

恕我直言,您無需連接程序代碼就能做的最好的事情是:

<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True" SourceName="LinkList">
        <Trigger.Setters>
            <Setter Property="BackColor" Value="Red" />
        </Trigger.Setters>
    </Trigger>
</ControlTemplate.Triggers>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM