5

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; }
        }
    }
}
moonlander
  • 143
  • 2
  • 8
  • Wouldn't it be easier, if you just take the TextBox, and on the designer right click and chose edit template -> edit copy, and then modify what you need to modify inside the template?. – adminSoftDK Oct 27 '16 at 14:48
  • yes, but i have other functionality in the code behind that i want to include in the custom text box. i didn't include it here for simplicity. and i want a reusable control with the combined code behind and the style changes functionality. anyhow, you know if it can be done like this in a dll library like i wanted? – moonlander Oct 27 '16 at 14:54
  • Sure it can, all you have to do is to, add new UserControl, and name it CustomTextBox (if this is what you want) and then replace the Tag UserControl with TextBox, and from this point you can use this TextBox just like the user control, but you will still have your code behind that you can access. I find this much easier than custom control. – adminSoftDK Oct 27 '16 at 15:05

1 Answers1

10

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.

adminSoftDK
  • 2,012
  • 1
  • 21
  • 41