2011-03-25 114 views
2

在混合使用TabControl元素爲Silverlight我創建了下面的標記:綁定路徑描邊色到前景

<controls:TabControl> 
    <controls:TabItem Header="TabItem" Style="{StaticResource TabItemStyle1}" /> 
    <controls:TabItem Style="{StaticResource TabItemStyle1}"> 
     <controls:TabItem.Header> 
      <StackPanel Orientation="Horizontal"> 
       <Path Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14" 
        StrokeLineJoin="Round" Margin="0 0 6 0" 
        Stroke="Black"/> 
       <TextBlock Text="TabItem"/> 
      </StackPanel> 
     </controls:TabItem.Header> 
    </controls:TabItem> 
</controls:TabControl> 

TabItemStyle1是的TabItem的默認樣式的副本。 我加入的MouseOver故事板的彩色動畫改變TabItemStyle1,這樣,當鼠標懸停他們未選擇的選項卡項目變成紅色:

<ColorAnimation BeginTime="0" Duration="00:00:00.001" 
    Storyboard.TargetName="HeaderTopUnselected" 
    Storyboard.TargetProperty="(UIElement.Foreground).(SolidColorBrush.Color)" 
    To="Red" /> 

現在,當我將鼠標懸停在第二個選項卡,文本變成紅色,但是路徑依然黑: Hovered second tab header

我該如何定義Path Stroke顏色以使其遵循相同的規則?

回答

0

我通過將標題內容畫筆綁定到{TemplateBinding TextElement.Foreground}來使其工作。

在其他情況下,我使用標準屬性綁定與轉換器,例如,如果我不得不調整元素的畫筆到項目狀態。

0

它不是一個完美的解決方案,但你可以使用這個

<sdk:TabControl> 
     <sdk:TabItem Header="item1"></sdk:TabItem> 
     <sdk:TabItem Foreground="Red" x:Name="someNameForTheTab"> 
      <sdk:TabItem.Header> 
       <StackPanel Orientation="Horizontal"> 
        <!--Just set stroke binding to the foreground of the tabItem--> 
        <Path Stroke="{Binding Foreground, ElementName=someNameForTheTab}" Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14"      
          StrokeLineJoin="Round" Margin="0 0 6 0"/> 
        <TextBlock Text="item2"/> 
       </StackPanel> 
      </sdk:TabItem.Header> 
     </sdk:TabItem> 
    </sdk:TabControl> 
+0

這隻適用於您在TabItem上明確設置的Foreground。動畫中改變的顏色仍然只適用於文本,而不適用於路徑。 – Mart 2011-03-25 12:53:58

2

下面應該工作:

<controls:TabControl> 
    <controls:TabItem Header="TabItem" Style="{StaticResource TabItemStyle1}" /> 
    <controls:TabItem Style="{StaticResource TabItemStyle1}"> 
     <controls:TabItem.Header> 
      <StackPanel Orientation="Horizontal"> 
       <Path Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14" 
        StrokeLineJoin="Round" Margin="0 0 6 0" 
        Stroke="{Binding ElementName=textBlock, Path=Foreground}"/> 
       <TextBlock x:Name="textBlock" Text="TabItem"/> 
      </StackPanel> 
     </controls:TabItem.Header> 
    </controls:TabItem> 
</controls:TabControl> 
+0

它適用於初始顏色,但不會對由動畫更改的顏色作出反應。任何人都可以解釋TextBlock是如何工作的,因爲它在這種情況下行爲正確嗎? – Mart 2011-03-25 13:36:42

+0

@ Mart-不幸的是,使用Silverlight時,該代碼全部隱藏。這篇文章(http://msdn.microsoft.com/en-us/library/cc189010(v = vs.95).aspx)描述了繼承行爲,它基本上包括Controls和TextBlocks。我會盡力在稍後更詳細地查看您的示例。 – CodeNaked 2011-03-25 13:49:47

0

嘗試像這樣綁定到TemplatedParent:

<Path 
Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14" 
StrokeLineJoin="Round" 
Margin="0 0 6 0" 
Stroke="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"/> 

我的天堂沒有測試過這個,但給它一個旋風,讓我知道。如果它不工作,試試這個:

<Path Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14" StrokeLineJoin="Round" Margin="0 0 6 0"> 
    <Path.Stroke> 
     <SolidColorBrush Color="{Binding Foreground.Color, RelativeSource={RelativeSource TemplatedParent}}" /> 
    </Path.Stroke> 
</Path> 

我有一個感覺,顏色屬性需要具有約束力,而不是實際刷的來源。

+0

我嘗試了兩個變種沒有成功。第一次顯示或MouseOver時,路徑根本不會被繪製。但我相信畫筆和顏色之間可能存在不匹配...... – Mart 2011-03-26 11:03:35

+0

您擁有的故事板正在更改SolidColorBrush.Color屬性。所以我認爲你需要弄清楚如何與之共享一個綁定。另外,TextBlocks有一些神奇的層疊前景顏色綁定。我想你可能需要創建自己的控件,它繼承自TabItem並添加一個新的Icon內容屬性。 – Laith 2011-03-27 07:24:56

-1

// animazione periferica

public static void LineAnimation(Line _line,String _colore) 
    { 
     Storyboard result = new Storyboard(); 
     Duration duration = new Duration(TimeSpan.FromSeconds(2)); 

     ColorAnimation animation = new ColorAnimation(); 
     animation.RepeatBehavior = RepeatBehavior.Forever; 
     animation.Duration = duration; 
     switch (_colore.ToUpper()) 
     { 
      case "RED": 
       animation.From = Colors.Red; 
       break; 
      case "ORANGE": 
       animation.From = Colors.Orange; 
       break; 
      case "YELLOW": 
       animation.From = Colors.Yellow; 
       break; 
      case "GRAY": 
       animation.From = Colors.DarkGray; 
       break; 
      default: 
       animation.From = Colors.Green; 
       break; 
     } 

     animation.To = Colors.Gray; 
     Storyboard.SetTarget(animation, _line); 
     Storyboard.SetTargetProperty(animation, new PropertyPath("(Line.Stroke).(SolidColorBrush.Color)")); 
     result.Children.Add(animation); 
     result.Begin(); 

    } 
} 

// ************ *************

public partial class MainPage : UserControl 
{ 
    public Line _line; 

    public MainPage() 
    { 
     InitializeComponent(); 
     Canvas.MouseLeftButtonDown += Canvas_MouseLeftButtonDown; 
     Canvas.MouseLeftButtonUp += Canvas_MouseLeftButtonUp; 
    } 

    void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     _line.X2 = e.GetPosition(this.Canvas).X; 
     _line.Y2 = e.GetPosition(this.Canvas).Y; 
     _line.Loaded += _line_Loaded; 
     Canvas.Children.Add(_line); 
    } 

    void _line_Loaded(object sender, RoutedEventArgs e) 
    { 
     Cls_Barriere.LineAnimation(sender as Line, "RED"); 
    } 

    void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     _line = new Line(); 
     _line.Stroke = new SolidColorBrush(Colors.White); 
     _line.StrokeThickness = 5; 
     _line.StrokeStartLineCap = PenLineCap.Round; 
     _line.StrokeEndLineCap = PenLineCap.Round; 
     _line.StrokeDashCap = PenLineCap.Round; 

     _line.X1 = e.GetPosition(this.Canvas).X; 
     _line.Y1= e.GetPosition(this.Canvas).Y; 

    } 

    private void UserControl_Loaded(object sender, RoutedEventArgs e) 
    { 

    } 
}