开发者

Bind a Path Stroke color to Foreground

Using the TabControl element for Silverlight in Blend I created the following markup:

<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 is a copy of the default style of a TabItem. I altered TabItemStyle1 by adding a color animation in the MouseOver storyboard so that unselected tab items become red when the mouse hovers them:

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

Now when I hover the second tab, the text becomes red but the Path remains black:

Bind a Path Stroke color to Foreground

Ho开发者_StackOverfloww should I define the Path Stroke color to make it follow the same rule?


The following should work:

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


it's not a perfect solution but you could use this

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


Try binding to the TemplatedParent like this:

<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}}"/>

I haven't tested this, but give it a whirl and let me know. If it doesn't work, try this:

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

I have a feeling that the Color property needs to be the source of binding, not the actual brush.


I made it work by binding the header content brushes to {TemplateBinding TextElement.Foreground}.

In other cases I used standard property binding with converters, for example if I had to adapt element's brushes to item state.


// 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)
    {

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜