[英]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.