简体   繁体   English

当自定义控件从工具箱拖放到Mainwindow时,更改XAML自动生成文本

[英]Change the XAML auto generating text when a custom control is dropped from the toolbox onto Mainwindow

When a wpf custom control is added to the toolbox and dragged onto the MainWindow, the automatically generated text in the XAML editor contains some properties=values by default. 将wpf自定义控件添加到工具箱中并将其拖动到MainWindow时,默认情况下,XAML编辑器中自动生成的文本包含一些properties = values。

How can I alter this text so that automatically includes some new properties of my custom control and/or remove others? 如何更改此文本,以便自动包含自定义控件的某些新属性和/或删除其他属性?

You can get pretty flexible design time behavior with a combination of System.ComponentModel attributes and DependencyProperty metadata. 通过结合使用System.ComponentModel属性和DependencyProperty元数据,可以获得非常灵活的设计时行为。 The PropertyMetadata class has a constructor that takes a default value: PropertyMetadata类具有一个采用默认值的构造函数:

[Category("MyCustomCategory")]
public string MyCustomProperty
{
    get { return GetValue(MyCustomPropertyProperty).ToString(); }
    set { SetValue(MyCustomPropertyProperty, value); }
}
public static DependencyProperty MyCustomPropertyProperty =
    DependencyProperty
    .Register(
        "MyCustomProperty",
        typeof(string),
        typeof(MyCustomUserControl),
        new PropertyMetadata("My default value")); // <--- default value

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM