积极答复者
类对应的xaml文件做UI描述

问题
-
请问如何 设计一个控件类的 DefaultStyleKey = 就是我在类里面写一个控件
比如 继承一个 Control 但我需要 从新定义它的UI . 所以就建立一个同名的xaml文件
然后DefaultStyleKey = 。。这个同名的xaml. 就是 toolkit中那些自定控件那样做的。
但为什么我这边老是xaml 设计的UI没有起作用
答案
-
Hi~
不能是同名的xaml,还要有些其它的约束。
你得在你的SL类库项目下新建一个名为Themes的文件夹。注意,一定要为Themes,然后在这个文件夹中新建名为Generic.xaml的文件作为UI描述文件,
注意,名称也不能变更。然后DefaultStyleKey=“”
举例:
Generic.xaml文件:<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Daisy="clr-namespace:Daisy;assembly=Daisy.VerificationCode" > <Style TargetType="Daisy:VerificationCode"> <Setter Property="Width" Value="150"></Setter> <Setter Property="Height" Value="50"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Daisy:VerificationCode"> <Grid x:Name="RootElement"> <Image x:Name="img_PIC" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <ToolTipService.ToolTip> <TextBlock>看不清?单击刷新验证码</TextBlock> </ToolTipService.ToolTip> </Image> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
对应的VerificationCode.cs文件:
using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace Daisy { public class VerificationCode:Control { private Image Img_PIC; public VerificationCode() { this.DefaultStyleKey = typeof(VerificationCode); this.MouseLeftButtonDown += new MouseButtonEventHandler(RefreashPIC); } //注册依赖属性Code,其用来存储区分大小写的当前产生的验证码字符串. public static readonly DependencyProperty CodeProperty = DependencyProperty.Register("Code", typeof(string), typeof(VerificationCode), null); public string Code { get { return (string)GetValue(CodeProperty); } set { SetValue(CodeProperty, value); } } //调用VerificationCode_Lib类产生验证码 private void CreatePIC() { VerificationCode_Lib VC_Lib = new VerificationCode_Lib(); VC_Lib.CreatImage(Code=VC_Lib.CreateVerificationCode(5),Img_PIC, 150, 50); } //获取模板子对象及初次产生验证码 public override void OnApplyTemplate() { base.OnApplyTemplate(); Img_PIC = (Image)GetTemplateChild("img_PIC"); CreatePIC(); } //处理用户单击事件(刷新验证码) private void RefreashPIC(object sender, MouseEventArgs e) { CreatePIC(); } } }
这是我写的一个验证码控件。
好运~朋友
Please remember to mark the replies as answers if they help or unmark them if they provide no help.- 已标记为答案 worldman 2010年6月9日 15:03
全部回复
-
Hi~
不能是同名的xaml,还要有些其它的约束。
你得在你的SL类库项目下新建一个名为Themes的文件夹。注意,一定要为Themes,然后在这个文件夹中新建名为Generic.xaml的文件作为UI描述文件,
注意,名称也不能变更。然后DefaultStyleKey=“”
举例:
Generic.xaml文件:<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Daisy="clr-namespace:Daisy;assembly=Daisy.VerificationCode" > <Style TargetType="Daisy:VerificationCode"> <Setter Property="Width" Value="150"></Setter> <Setter Property="Height" Value="50"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Daisy:VerificationCode"> <Grid x:Name="RootElement"> <Image x:Name="img_PIC" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <ToolTipService.ToolTip> <TextBlock>看不清?单击刷新验证码</TextBlock> </ToolTipService.ToolTip> </Image> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
对应的VerificationCode.cs文件:
using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace Daisy { public class VerificationCode:Control { private Image Img_PIC; public VerificationCode() { this.DefaultStyleKey = typeof(VerificationCode); this.MouseLeftButtonDown += new MouseButtonEventHandler(RefreashPIC); } //注册依赖属性Code,其用来存储区分大小写的当前产生的验证码字符串. public static readonly DependencyProperty CodeProperty = DependencyProperty.Register("Code", typeof(string), typeof(VerificationCode), null); public string Code { get { return (string)GetValue(CodeProperty); } set { SetValue(CodeProperty, value); } } //调用VerificationCode_Lib类产生验证码 private void CreatePIC() { VerificationCode_Lib VC_Lib = new VerificationCode_Lib(); VC_Lib.CreatImage(Code=VC_Lib.CreateVerificationCode(5),Img_PIC, 150, 50); } //获取模板子对象及初次产生验证码 public override void OnApplyTemplate() { base.OnApplyTemplate(); Img_PIC = (Image)GetTemplateChild("img_PIC"); CreatePIC(); } //处理用户单击事件(刷新验证码) private void RefreashPIC(object sender, MouseEventArgs e) { CreatePIC(); } } }
这是我写的一个验证码控件。
好运~朋友
Please remember to mark the replies as answers if they help or unmark them if they provide no help.- 已标记为答案 worldman 2010年6月9日 15:03