将路径描边颜色绑定到前景

时间:2011-03-25 11:02:17

标签: silverlight-4.0 controltemplate

在Blend中使用Silverlight的TabControl元素我创建了以下标记:

<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>

TabItemStyle1TabItem默认样式的副本。 我通过在TabItemStyle1故事板中添加颜色动画来更改MouseOver,以便当鼠标悬停时,未选中的标签项会变为红色:

<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颜色以使其遵循相同的规则?

5 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>

答案 2 :(得分: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>

我觉得Color属性需要是绑定的来源,而不是实际的刷子。

答案 3 :(得分:0)

我通过将标题内容画笔绑定到{TemplateBinding TextElement.Foreground}来实现它。

在其他情况下,我使用标准属性绑定转换器,例如,如果我必须将元素的画笔调整为项目状态。

答案 4 :(得分:-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)
    {

    }
}