Silverlight 控件自定义样式

Silverlight 控件自定义样式,第1张

概述<Style  x:Key="PopupTextBoxStyle"  TargetType="Grid">             <Setter Property="Width" Value="200"/>             <Setter Property="Template">                 <Setter.Value>                     <Co

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

总结

以上是内存溢出为你收集整理的Silverlight 控件自定义样式全部内容,希望文章能够帮你解决Silverlight 控件自定义样式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1025453.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存