Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; }
        }
    }
}
like image 715
moonlander Avatar asked Jan 05 '23 03:01

moonlander


1 Answers

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.

like image 174
adminSoftDK Avatar answered Jan 10 '23 12:01

adminSoftDK