<Style x:Key="PopupTextBoxStyle" TargetType="Grid">
<Setter Property="Width" Value="200"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Grid">
<Grid>
<Rectangle Width="100" Height="50">
</Rectangle>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
试图给Grid定义Template属性样式是不对的,因为Grid没有Template属性,Grid继承自Panel,Panel/Canvas也没有Template属性。只有继承自Control类型的控件才有Template属性例如Button、UserControl。
如果想实现这种功能,可以定义一个用户控件,设置在generic中设置默认样式,或者定义一个其他控件,在app中定义其样式,如下:
You probably should build a custom control for this Node instead of a UserControl. For custom control you define the default look of this control in the generic.xaml file instead of App.xaml.
Add a class file called Node.cs:
public class Node : Control
{
public Node()
{
this.DefaultStyleKey = typeof(Node);
}
}
Add the following XAML to the Themes/generic.xaml file
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:YourNodeControlsNameSpace;assembly=YourNodeControlAssemblyName"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
>
<Style TargetType="controls:Node">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:Node">
<Ellipse x:Name="NodeImage" Stroke="Green" StrokeThickness="2" Fill="Transparent" Width="6" Height="6"></Ellipse>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Now you can use your Node control in your application. You can also define Templates in App.xaml for your Node control to give it different look if you need to. Otherwise, they have default look defined in your generic.xaml.