Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 4.0 将路径笔划颜色绑定到前景_Silverlight 4.0_Controltemplate - Fatal编程技术网

Silverlight 4.0 将路径笔划颜色绑定到前景

Silverlight 4.0 将路径笔划颜色绑定到前景,silverlight-4.0,controltemplate,Silverlight 4.0,Controltemplate,在Blend中使用Silverlight的TabControl元素,我创建了以下标记: <controls:TabControl> <controls:TabItem Header="TabItem" Style="{StaticResource TabItemStyle1}" /> <controls:TabItem Style="{StaticResource TabItemStyle1}"> <controls:Ta

在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>
现在,当我将鼠标悬停在第二个选项卡上时,文本变为红色,但路径仍为黑色:


如何定义路径笔划颜色以使其遵循相同的规则?

这不是一个完美的解决方案,但您可以使用此选项

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

以下各项应起作用:

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

尝试绑定到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属性应该是绑定的源,而不是实际的笔刷。

我通过将标题内容笔刷绑定到
{TemplateBinding textfelement.Foreground}
来实现


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

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

    }
}

这仅适用于在TabItem上显式设置的前景。动画中更改的颜色仍然只适用于文本,而不是路径。它适用于初始颜色,但不会对动画更改的颜色作出反应。任何人都能解释TextBlock是如何工作的,因为它在这个场景中的行为是正确的吗?@Mart-不幸的是,对于Silverlight,这些代码都是隐藏的。这描述了继承行为,基本上包括控件和文本块。稍后我将尝试更详细地查看您的示例。我尝试了两种变体,但均未成功。在第一次显示或鼠标悬停时,根本不会绘制路径。但我相信画笔和颜色之间可能存在不匹配…您拥有的故事板正在更改SolidColorBrush.Color属性。所以我认为你需要弄清楚如何与它共享一个绑定。此外,文本块具有某种神奇的级联前景颜色绑定。我认为您可能必须创建自己的控件,该控件继承自TabItem,并添加一个新的图标内容属性。
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)
    {

    }
}