2014-09-10 112 views
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中设置它时没有任何影响。

+0

你可能忘了写上“线下”。 – 2014-09-10 19:09:30

回答

1

恕我直言,你可以无需在接线过程代码做的最好的是:

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