如何解决将路径描边颜色绑定到前景
| 使用Blend中的Silverlight的“ 0”元素,我创建了以下标记:<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 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\" />
现在,当我将鼠标悬停在第二个选项卡上时,文本变为红色,但路径仍为黑色:
如何定义“路径描边”颜色以使其遵循相同规则?
解决方法
以下应该工作:
<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 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>
, 这不是一个完美的解决方案,但是您可以使用它
<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,14\"
StrokeLineJoin=\"Round\" Margin=\"0 0 6 0\"/>
<TextBlock Text=\"item2\"/>
</StackPanel>
</sdk:TabItem.Header>
</sdk:TabItem>
</sdk:TabControl>
, 尝试像这样绑定到TemplatedParent:
<Path
Data=\"M0,14\"
StrokeLineJoin=\"Round\"
Margin=\"0 0 6 0\"
Stroke=\"{Binding Foreground,RelativeSource={RelativeSource TemplatedParent}}\"/>
我尚未对此进行测试,但请稍等一下,然后通知我。如果不起作用,请尝试以下操作:
<Path Data=\"M0,14\" StrokeLineJoin=\"Round\" Margin=\"0 0 6 0\">
<Path.Stroke>
<SolidColorBrush Color=\"{Binding Foreground.Color,RelativeSource={RelativeSource TemplatedParent}}\" />
</Path.Stroke>
</Path>
我觉得Color属性必须是绑定的来源,而不是实际的画笔。
, 我通过将标题内容画笔绑定到{TemplateBinding TextElement.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)
{
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。