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

发布于 2024-10-26 10:03:44 字数 1260 浏览 7 评论 0原文

在 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 默认样式的副本。 我通过在 MouseOver 情节提要中添加颜色动画来更改 TabItemStyle1,以便当鼠标悬停在未选定的选项卡项上时它们会变成红色:

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

现在,当我悬停第二个选项卡时,文本会变为红色红色但路径保持黑色: 悬停第二个选项卡标题

我应该如何定义路径描边颜色以使其遵循相同的规则?

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:
Hovered second tab header

How should I define the Path Stroke color to make it follow the same rule?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

┼── 2024-11-02 10:03:44

以下应该有效:

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

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>
半山落雨半山空 2024-11-02 10:03:44

这不是一个完美的解决方案,但你可以使用这个

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

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>
哆啦不做梦 2024-11-02 10:03:44

尝试像这样绑定到 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 属性需要成为绑定的源,而不是实际的画笔。

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.

裸钻 2024-11-02 10:03:44

我通过将标题内容画笔绑定到 {TemplateBinding TextElement.Foreground} 来使其工作。

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

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.

白芷 2024-11-02 10:03:44

// 动画

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

    }
}

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

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文