我想創建一個簡單的自定義控件,它可以擴展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