1
我有一個ResourceDictionary
,它有一個Style
負責創建垂直側面菜單的樣式。 下面是創建演示此問題一個假設的例子的XAML ResourceDictionary
:如何在WPF的ListBoxItem上觸發一個觸發器時設置父元素的屬性
<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>
風格是基於一個名爲Controle
類,從Control
繼承。 下面是這個類的代碼:
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中設置它時沒有任何影響。
你可能忘了寫上「線下」。 – 2014-09-10 19:09:30