【发布时间】:2017-12-17 13:25:54
【问题描述】:
WPF .Net 4.5
"在'System.Windows.Controls.Grid'的名称范围内找不到'backgroundBrush'名称。"
我有一个内容控件,它通过情节提要为路径元素设置动画。这在我的显示器右侧的一个侧栏中正常工作,很好地包含在 StackPanel 中。
我现在想将它拖放到 InkCanvas 上,而不会将其从当前位置移除。显然,WPF 可视化树不允许这样做,所以我需要克隆它。
我发现的最好的(?)克隆方法(由 StackOverflow 提供)是:
private void IC_Drop(object sender, DragEventArgs e)
{
InkCanvas ic = sender as InkCanvas;
var ekg = e.Data.GetData("EKG") as ContentControl;
if (ekg != null)
{
var sb = new StringBuilder();
var writer = XmlWriter.Create(sb, new XmlWriterSettings
{
Indent = true,
ConformanceLevel = ConformanceLevel.Fragment,
OmitXmlDeclaration = true,
NamespaceHandling = NamespaceHandling.OmitDuplicates,
});
var mgr = new XamlDesignerSerializationManager(writer);
// HERE BE MAGIC!!!
mgr.XamlWriterMode = XamlWriterMode.Expression;
// THERE WERE MAGIC!!!
XamlWriter.Save(ekg, mgr);
StringReader stringReader = new StringReader(sb.ToString());
XmlReader xmlReader = XmlReader.Create(stringReader);
ContentControl newECG = (ContentControl)XamlReader.Load(xmlReader);
ic.Children.Add(newECG);
}
}
但是,这样做会导致上述错误。
如何克隆这个内容控件?或者克隆失败,有没有办法实现和动画拖拽路径元素一样的效果?
以下是心电图/心电图的定义:
<ContentControl Width="130" Height="130" PreviewMouseDown="ContentControl_PreviewMouseDown" >
<ContentControl.LayoutTransform>
<ScaleTransform ScaleX=".4" ScaleY=".4"/>
</ContentControl.LayoutTransform>
<!--Must set IsHitTestVisible to True to allow tunneling/bubbeling of Mouse Events on the ContentControl.-->
<Grid IsHitTestVisible="True">
<!--FrameBackground is the gray background upon which the green oscilliscope is placed-->
<Border CornerRadius="5" BorderBrush="#E0E0E0" BorderThickness="0,1,0,0" Background="{DynamicResource FrameBackground}" />
<Border BorderBrush="Black" BorderThickness="0,0,0,1" CornerRadius="5" />
<Border Background="{StaticResource MyGridBrushResource}" CornerRadius="5" Margin="5,5,5,25" IsHitTestVisible="False" />
<Border BorderBrush="#E0E0E0" BorderThickness="0,0,1,1" CornerRadius="5" Margin="5,5,5,25"/>
<Border BorderBrush="#303030" BorderThickness="1,1,0,0" CornerRadius="5" Margin="5,5,5,25">
<Grid>
<Grid.Resources>
<Style TargetType="Line">
<Setter Property="Stroke" Value="Lime" />
<Setter Property="Opacity" Value="0.5" />
</Style>
<Storyboard x:Key="OnLoaded">
<DoubleAnimation From="0" To="1.0"
RepeatBehavior="Forever"
Duration="00:00:2"
Storyboard.TargetName="backgroundBrush"
Storyboard.TargetProperty="GradientStops[2].Offset">
</DoubleAnimation>
<DoubleAnimation From="0.01" To="1.01"
RepeatBehavior="Forever"
Duration="00:00:2"
Storyboard.TargetName="backgroundBrush"
Storyboard.TargetProperty ="GradientStops[3].Offset">
</DoubleAnimation>
<DoubleAnimation From="-0.9" To="0.1"
RepeatBehavior="Forever"
Duration="00:00:2"
Storyboard.TargetName="backgroundBrush"
Storyboard.TargetProperty ="GradientStops[1].Offset">
</DoubleAnimation>
</Storyboard>
</Grid.Resources>
<Grid.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource OnLoaded}"/>
</EventTrigger>
</Grid.Triggers>
<Grid Background="Transparent">
<Border BorderBrush="#00F300" BorderThickness="0" Margin="10">
<Path Name="polyLine" StrokeThickness="1" Stretch="Fill" Margin="5"
Data="M 0,10 4,10 6,8 7,10 9,10 10,11, 11,1 12,13 13,10 18,10 21,8 24,10
30,10 34,10 36,8 37,10 39,10 40,11, 41,1 42,13 43,10 48,10 51,8 54,10">
<Path.Stroke>
<LinearGradientBrush x:Name="backgroundBrush" StartPoint="0,0" EndPoint="1,0" SpreadMethod="Repeat">
<GradientStop Color="Transparent" Offset ="0.0" />
<GradientStop Color="Transparent" Offset ="0.0" />
<GradientStop Color="#00FF00" Offset ="0.0"/>
<GradientStop Color="Transparent" Offset ="0.0" />
<GradientStop Color="Transparent" Offset ="1"/>
</LinearGradientBrush>
</Path.Stroke>
</Path>
</Border>
</Grid>
</Grid>
</Border>
</Grid>
</ContentControl>
任何帮助或建议将不胜感激。
TIA
【问题讨论】:
标签: wpf xaml storyboard clone contentcontrol