moonlander moonlander - 1 month ago 29
C# Question

Extend TextBox control and change part of default style

I want to create a class library (dll file) of the WPF TextBox with extended functionality. But i want to change one part of the TextBox's default style (IsMouseOver property trigger).

I created a new WPF user control library project, deleted the generated .XAML and .cs files from it, and added a new class file. Then i derived from the TextBox class, but i don't know how to access the style XAML.

I can't figure out how this supposed to be done..

Inside my project i currently have only this .cs file, and no .XAML file:

namespace CustomControls
{
public class CustomTextBox : TextBox
{
private string customProperty;
public string CustomProperty
{
get { return customProperty; }
set { customProperty = value; }
}
}
}

Answer

You can do something like this

<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>

Code behind

public partial class CustomTextBox : TextBox
{
    public CustomTextBox()
    {
        InitializeComponent();
    }
}

Now you can do whatever you want to do in your xaml (edit template, apply style etc), and you'll have access to it from code behind.