WPF自定义的派生的控件样式 [英] WPF custom derived control style

查看:340
本文介绍了WPF自定义的派生的控件样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有按钮派生的自定义控制:

 类MyControl:按钮{} 

和猜想,这个类是空的(没有成员)。



在应用程序的主窗口资源,我用一个包含大多数 WPF控件(所谓的主题)样式的ResourceDictionary:

 < ResourceDictionary中源=BureauBlue.xaml/> 



所以,在窗口看都像控制它在这一主题文件中定义。但风格不会受到影响的MyControl控件。我该怎么办MyControl看起来一样的Button控件



更​​新:在BureauBlue.xaml按钮的样式没有密钥,并在下面的方式定义:

 <风格的TargetType ={X:类型按钮}支持算法FMP ={X:空}> ...< /样式和GT; 


解决方案

您覆盖DefaultStyleKey在你的静态构造函数的元数据:

 静态MyControl()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof运算(MyControl),
新FrameworkPropertyMetadata(typeof运算(MyControl)));
}



然后,在你的资源,你可以依据其对按钮样式:

 <风格的TargetType ={X:类型拼箱:MyControl}支持算法FMP ={StaticResource的{X:类型按钮}} /> 



我试过在过去的覆盖DefaultStyleKey的元数据指向基类(巴顿在你的情况),但它似乎并没有工作。


I have a custom control derived from Button:

    class MyControl : Button{}

And suppose, this class is empty (has no members).

In the application's main window resources I use ResourceDictionary that contains styles for most WPF controls (so called theme):

    <ResourceDictionary Source="BureauBlue.xaml" />

So, all controls on the window look like it is defined in that theme file. But the styles are not affected on MyControl controls. How can I do MyControl to look same as a Button controls?

Update: The style for Button in BureauBlue.xaml has no key and is defined in the following way:

    <Style TargetType="{x:Type Button}" BasedOn="{x:Null}"> ...</Style>

解决方案

You override the DefaultStyleKey's metadata in your static constructor:

static MyControl()
{
    DefaultStyleKeyProperty.OverrideMetadata(
        typeof(MyControl),
        new FrameworkPropertyMetadata(typeof(MyControl)));
}

Then, in your resources, you can base its style on the button:

<Style TargetType="{x:Type lcl:MyControl}" BasedOn="{StaticResource {x:Type Button}}" />

I've tried in the past to override the DefaultStyleKey's metadata to point to the base class (Button in your case), but it doesn't seem to work.

这篇关于WPF自定义的派生的控件样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆