Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF,当直接从控件继承时,如何设置默认属性,如';背景';密码?_Wpf_Controls - Fatal编程技术网

WPF,当直接从控件继承时,如何设置默认属性,如';背景';密码?

WPF,当直接从控件继承时,如何设置默认属性,如';背景';密码?,wpf,controls,Wpf,Controls,由于某些特殊原因,我试图避免在此控件中使用XAML 然后我创建了一个名为MyControl的新C#类 class MyControl: Control { public MyControl() { this.BorderBrush = SystemColors.ActiveBorderBrush; this.Background = Brushes.Blue; this.BorderThickness = new Thickness

由于某些特殊原因,我试图避免在此控件中使用XAML

然后我创建了一个名为MyControl的新C#类

class MyControl: Control
{
    public MyControl()
    {
        this.BorderBrush = SystemColors.ActiveBorderBrush;
        this.Background = Brushes.Blue;
        this.BorderThickness = new Thickness(1);
    }
}
我在XAML中创建了一个WPF窗口,并添加了

xmlns:local="clr-namespace:....."
<local:MyControl Height="186" HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="mycontrol" VerticalAlignment="Top" Width="331" />
xmlns:local=“clr命名空间:…”
但是背景属性不起作用。
任何人都可以帮我吗?

从下面的备注部分:

没有ControlTemplate的控件在应用程序中不可见 应用程序,并且设置以下属性无效,除非 ControlTemplate显式引用它们:

  • 背景
  • 毛刷
  • 边界厚度
  • 丰特家族
  • 字体大小
  • 丰特朗
  • 容重
  • 前景
  • 水平对齐
  • 垂直内容对齐

你的控件需要一个模板,否则它根本不会显示…所以我必须创建一个ControlTemplate?如果不使用XAML,在构造函数中创建ControlTemplate是否是一个好主意?@yylang1987,为什么不使用XAML?尝试将自定义控件(WPF)添加到Visual Studio项目中,看看它能做什么。在代码中创建ControlTemplate没有多大意义。您也可以从更专门的控件(如标签)派生,这取决于您的控件应该是什么。我强烈建议您从
UserControl
开始,并熟悉它的XAML。