我正在创建一系列 ControlTemplates 以从某些字符串显示在 WPF 控件上。我使用这段代码:
string theTemplate = @"<ControlTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<Grid Name=""RootElement"" RenderTransformOrigin=""0.5,0.5"" >
<Grid.RenderTransform>
<ScaleTransform ScaleX=""1"" ScaleY=""1"" />
</Grid.RenderTransform>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name=""CommonStates"">
<VisualState Name=""Normal"">
<Storyboard>
<DoubleAnimation BeginTime=""00:00:00""
Storyboard.TargetName=""RootElement""
Storyboard.TargetProperty=""(UIElement.RenderTransform).(ScaleTransform.ScaleX)""
To=""1"" Duration=""0:0:0.1"" />
<DoubleAnimation BeginTime=""00:00:00""
Storyboard.TargetName=""RootElement""
Storyboard.TargetProperty=""(UIElement.RenderTransform).(ScaleTransform.ScaleY)""
To=""1"" Duration=""0:0:0.1"" />
</Storyboard>
</VisualState>
<VisualState Name=""MouseOver"">
<Storyboard>
<DoubleAnimation BeginTime=""00:00:00""
Storyboard.TargetName=""RootElement""
Storyboard.TargetProperty=""(UIElement.RenderTransform).(ScaleTransform.ScaleX)""
To=""1.5"" Duration=""0:0:0.1"" />
<DoubleAnimation BeginTime=""00:00:00""
Storyboard.TargetName=""RootElement""
Storyboard.TargetProperty=""(UIElement.RenderTransform).(ScaleTransform.ScaleY)""
To=""1.5"" Duration=""0:0:0.1"" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Ellipse Height=""{Binding Symbol.Size}"" Width=""{Binding Symbol.Size}"" Fill=""{Binding Symbol.Color}"" Stroke=""Black"">
</Ellipse>
</Grid>
</ControlTemplate>";
System.IO.StringReader stringReader = new System.IO.StringReader(theTemplate);
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(stringReader);
ControlTemplate ct = (ControlTemplate)System.Windows.Markup.XamlReader.Load(reader);
这一切都很好。问题是当我尝试使用我的自定义形状而不是使用椭圆时。在常规 WPF 窗口中,我使用此参考:
xmlns:custom="clr-namespace:WpfApplication2"
然后我可以做的不是椭圆:
<custom:Square Size=""{Binding Symbol.Size}"" Fill=""{Binding Symbol.Color}"" Stroke=""Black"" RotationAngle=""0"">
</custom:Square>
现在,当我将该行添加到字符串“theTemplate”时,它现在可以工作了。我认为的原因是因为我没有该参考。我尝试在不同的地方添加参考,例如:
<ControlTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:custom="clr-namespace:WpfApplication2" >
并没有工作。我也试过:
<custom:Square Size=""{Binding Symbol.Size}"" Fill=""{Binding Symbol.Color}"" Stroke=""Black"" RotationAngle=""0"" xmlns:custom="clr-namespace:WpfApplication2" >
</custom:Square>
而且也不喜欢。
苏,有什么建议吗?如何引用我的自定义形状?
谢谢!