本文介绍了如何设置默认值继承自CustomControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从TextBox继承的CustomControl,当在设计时将其放置在窗口中时,它没有设置其 Width 属性。默认情况下,在设计时,文本框的 Width 值为120。

I have a CustomControl inherited from a TextBox which is not setting its Width property when place it to a window at design time. the TextBox required a Width of 120 at design time by default.

如何将控件的宽度设置为120

How can I set my control width to 120 by default?

解决方案对于控制的发展(如果存在)更正确

Solution more correct for development of control, if exist

推荐答案

您可以尝试使用类:

/*
** In your custom control class
*/

using Microsoft.Windows.Design.Features;
// ...

[Feature(typeof(CustomTextBoxDefaults))]
public class CustomTextBox : TextBox
{
    /* ... */
}

/*
** CustomTextBoxDefaults provides default values for designer
*/

using Microsoft.Windows.Design.Model;
// ...

class CustomTextBoxDefaults : DefaultInitializer
{
    public override void InitializeDefaults(ModelItem item)
    {
        item.Properties["Width"].SetValue(120);
        // Setup other properties
    }
}

请注意应该在项目中添加两个程序集:Microsoft.Windows.Design.Extensibility和Microsoft.Windows.Design.Interaction

Note you should add two assemblies to project: Microsoft.Windows.Design.Extensibility and Microsoft.Windows.Design.Interaction

您还可以阅读以下线程:

You may also read this thread: http://social.msdn.microsoft.com/Forums/en-US/1ce1184a-7b8c-40d1-ad52-71b2a6c9b3b6/wpf-designertoolbox-default-values?forum=vswpfdesigner

这篇关于如何设置默认值继承自CustomControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 01:06