0
我正在创建一系列ControlTemplates,以便从某些字符串显示在WPF控件上。我使用此代码:字符串XAML ControlTemplate
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>
而且也不喜欢它。
Sooo,有什么建议吗?我如何参考我的自定义形状?
谢谢!