简体   繁体   中英

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

I have a ResourceDictionary that has a Style responsible for creating the style of a vertical side menu. Below is the XAML ResourceDictionary of a hypothetical example that was created to demonstrate the problem:

<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>

The style is based on a class called Controle that inherits from Control . Below is the code of this class:

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); }
    }
}
}

I wanna know why in the line below:

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

I am not able to set a property of Controle ... The funny thing is that if I set the ban ownership of any other place, seem to take place, but when I'm inside the ItemTemplate by setting it has no effect.

IMHO the best you can do without wiring procedural code is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM