扩展TextBox控件并更改默认样式的一部分

moo*_*der 2 c# wpf custom-controls extending-classes

我想创建具有扩展功能的WPF TextBox的类库(dll文件)。但是我想更改文本框默认样式的一部分(IsMouseOver属性触发器)。

我创建了一个新的WPF用户控件库项目,从中删除了生成的.XAML和.cs文件,并添加了一个新的类文件。然后,我派生自TextBox类,但我不知道如何访问样式XAML。

我不知道应该怎么做。

在我的项目中,我目前仅具有此.cs文件,而没有.XAML文件:

namespace CustomControls
{
    public class CustomTextBox : TextBox
    {
        private string customProperty;
        public string CustomProperty
        {
            get { return customProperty; }
            set { customProperty = value; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

adm*_*tDK 6

你可以做这样的事情

<TextBox x:Class="CustomControls.MyFolder.CustomTextBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

</TextBox>
Run Code Online (Sandbox Code Playgroud)

后面的代码

public partial class CustomTextBox : TextBox
{
    public CustomTextBox()
    {
        InitializeComponent();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以在xaml中做任何想做的事情(编辑模板,应用样式等),并且可以从后面的代码中访问它。