2010-07-09 64 views
0

有人可以幫我解決這個問題嗎?我在WPF下面的模板設置:WPF:訪問ContentPresenter中的元素?

<Style TargetType="{x:Type Label}" x:Key="NavLink"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate x:Name="NavLinkControlTemplate" TargetType="{x:Type Label}"> 
        <Border x:Name="NavLinkBorder"> 
         <ContentPresenter x:Name="NavLinkContent" Margin="4,4,4,4" /> 
        </Border> 

        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter TargetName="NavLinkBorder" Property="Background" Value="#CCCCCC" /> 
          <Setter Property="Cursor" Value="Hand" /> 
         </Trigger> 
         <Trigger Property="IsMouseOver" Value="False"> 
          <Setter TargetName="NavLinkBorder" Property="Background" Value="#EAEAEA" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

當我的程序被加載時,它會自動創建充當導航菜單標籤的列表。從上面可以看到,當鼠標懸停在其中一個標籤上時,背景顏色會發生變化。唯一的問題是,我也有一個應用於標籤的上下文菜單,當我右鍵單擊以將其帶出時,標籤背景將返回到其原始顏色,而不是保留MouseOver顏色。

我谷歌搜索了大約一個小時左右,似乎無法找到觸發屬性,將檢查是否按下鼠標右鍵,所以我假設沒有一個。我在想,也許我可以通過代碼來實現這一點。

我曾嘗試下面的代碼,但我沒有任何運氣:

// this event is being added to each label at runtime... 
    tempLabel.MouseRightButtonUp += new MouseButtonEventHandler(NavLink_RightClicked); 

    // this is the method that the right-click calls... 
    private void NavLink_RightClicked(object sender, EventArgs e) 
     { 
      if (sender is Label) 
      { 
       currentContextLink = sender as Label; 

       // the below line won't work because the ControlTemplate seems to be overwriting it... 
       currentContextLink.Background = new SolidColorBrush(appFunctions.HexToColor("#FF0000")); 
      } 
     } 

我也試圖獲取標籤的父元素爲界,但似乎因爲它是建立通過模板,標籤的父親實際上是StackPanel,它包含了所有的標籤。

有人可以幫助我找出如何訪問邊框並更改其背景顏色,或者可能引導我在任何方向可能會幫助我完成此任務?

任何幫助非常感謝!

回答

1

您可以添加額外的數據觸發器,通過綁定來檢查ContextMenu的IsOpen屬性。

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=ContextMenu.IsOpen}" Value="True"> 
    <Setter TargetName="NavLinkBorder" Property="Background" Value="#CCCCCC" /> 
    <Setter Property="Cursor" Value="Hand" /> 
</DataTrigger> 

您也可以擺脫鼠標懸停=「假」觸發的,只是設置背景=「#EAEAEA」上NavLinkBorder當沒有觸發是活動的,將接管默認值。

或者,您可以將邊框顏色設置切換爲使用模板綁定,這將有助於您的代碼方法正常工作。無論如何,這是一個很好的做法,因爲它可以使您的模板更加靈活,因爲可以在各個Label實例上設置不同的背景值。以下是帶有一些TemplateBindings的樣式,並添加了上下文菜單觸發器。

<Style TargetType="{x:Type Label}" x:Key="NavLink"> 
    <Setter Property="Background" Value="#EAEAEA" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate x:Name="NavLinkControlTemplate" TargetType="{x:Type Label}"> 
       <Border x:Name="NavLinkBorder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}"> 
        <ContentPresenter x:Name="NavLinkContent" Margin="4,4,4,4" /> 
       </Border> 

       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter TargetName="NavLinkBorder" Property="Background" Value="#CCCCCC" /> 
         <Setter Property="Cursor" Value="Hand" /> 
        </Trigger> 
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=ContextMenu.IsOpen}" Value="True"> 
         <Setter TargetName="NavLinkBorder" Property="Background" Value="#CCCCCC" /> 
         <Setter Property="Cursor" Value="Hand" /> 
        </DataTrigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

約翰,非常感謝你!背景顏色現在保持;但是,仍然有一個小錯誤。由於我所有的標籤共享相同的上下文菜單,所以當它打開所有標籤上的背景顏色變化而不是我點擊的標籤時。有沒有一種方法來識別源元素,還是應該爲每個標籤創建單獨的上下文菜單? – 2010-07-12 13:33:41

+0

Nevermind,John,我能夠使用代碼來完成此操作,以確定何時打開和關閉上下文菜單並更改發件人的背景,就像您所說的那樣!非常感謝你! – 2010-07-12 18:59:07

0

在你的風格試試這個:

<EventTrigger RoutedEvent="MouseRightButtonUp"> 
    <BeginStoryboard> 
     <Storyboard> 
      <ColorAnimation To="#FF0000" Duration="0" Storyboard.TargetName="NavLinkBorder" Storyboard.TargetProperty="(Background).(Color)" /> 
     </Storyboard> 
    </BeginStoryboard> 
</EventTrigger> 

注意,<Trigger Property="IsMouseOver" Value="False">會改變顏色回#EAEAEA一旦移動鼠標時關閉。如果你想要的顏色,以「堅持」,修改ControlTemplate如下:

<Border x:Name="NavLinkBorder"> 
    <Border x:Name="NavLinkInnerBorder" Background="Transparent" > 
     <ContentPresenter x:Name="NavLinkContent" Margin="4,4,4,4" /> 
    </Border> 
</Border> 

...,改變ColorAnimation看起來像這樣:

<ColorAnimation To="#FF0000" Duration="0" Storyboard.TargetName="NavLinkInnerBorder" Storyboard.TargetProperty="(Background).(Color)" /> 
+0

請參閱下面的問題。這個評論框中沒有足夠的空間來發布這一切。 – 2010-07-09 20:57:02

0

謝謝你的幫助,但我對WPF來說有些新鮮,對RoutedEvents並不太好。我添加了第一部分代碼,但是當我運行它時,出現以下錯誤:

無法將屬性'RoutedEvent'中的字符串'MouseRightButtonUp'轉換爲'System.Windows.RoutedEvent'類型的對象。 RoutedEventConverter無法從System.String轉換。錯誤在標記文件對象System.Windows.EventTrigger 'MyProgram;組件/ frmmain.xaml' 線62的位置43

我的XAML代碼風格,現在看起來是這樣的:

<Style TargetType="{x:Type Label}" x:Key="NavLink"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Label}"> 
        <Border x:Name="NavLinkBorder" Background="#CCCCCC"> 
         <ContentPresenter x:Name="NavLinkContent" Margin="4,4,4,4" /> 
        </Border> 

        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter TargetName="NavLinkBorder" Property="Background" Value="#CCCCCC" /> 
          <Setter Property="Cursor" Value="Hand" /> 
         </Trigger> 
         <Trigger Property="IsMouseOver" Value="False"> 
          <Setter TargetName="NavLinkBorder" Property="Background" Value="#EAEAEA" /> 
         </Trigger> 
         <EventTrigger RoutedEvent="MouseRightButtonUp"> 
          <BeginStoryboard> 
           <Storyboard> 
            <ColorAnimation To="#FF0000" Duration="0" Storyboard.TargetName="NavLinkBorder" Storyboard.TargetProperty="(Background).(Color)" /> 
           </Storyboard> 
          </BeginStoryboard> 
         </EventTrigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

而且,之前我沒有張貼此,但這是什麼代碼看起來像生成我的標籤:

Label tempLabel = new Label(); 
    tempLabel.Content = "My Link"; 
    tempLabel.ContextMenu = (ContextMenu)FindResource("NavContextMenu"); 
    tempLabel.FontSize = 12; 
    tempLabel.Foreground = new SolidColorBrush(appFunctions.HexToColor("#000057")); 
    tempLabel.Name = "myNavLink"; 
    tempLabel.Style = (Style)FindResource("NavLink"); 

    tempLabel.MouseLeftButtonUp += new MouseButtonEventHandler(NavLink_LeftClicked); 
    tempLabel.MouseRightButtonUp += new MouseButtonEventHandler(NavLink_RightClicked); 

    navPanel.Children.Add(tempLabel); 

當我得到的錯誤,它強調從上面這行代碼:

tempLabel.Style = (Style)FindResource("NavLink"); 

也許我沒有正確附加標籤的樣式......?