我想创建一个简单的自定义控件,它可以扩展TextBox
。WPF自定义控件是空的
我通过Add -> New Item... -> Custom Control
创建它,我对自动生成的代码进行了一些更改。我将CustomControl
的基类更改为TextBox
,并删除Theme/Generic.xaml
文件中的Template
setter。
但是,当我将它添加到MainWindow
并运行时,它是空白的。这是我的最终代码:
文件Theme/Generic.xaml
:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test">
<Style TargetType="{x:Type local:CustomControl}">
<Setter Property="BorderThickness" Value="10"/>
</Style>
</ResourceDictionary>
文件CustomControl.cs
:
namespace Test
{
public class CustomControl : TextBox
{
static CustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
}
}
}
的问题是,你忘了初始化控制。 – macieqqq
你想通过使用自定义控件来实现什么?为了造型,你可以使用一种风格,例如 - 继承的构成。 –
@macieqqq以及如何初始化? – crupest