Silverlight 控件自定义样式

<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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值