Vallo
Vallo

Reputation: 1967

polymorphism for external package

In my project I included an external package for the control AutoCompleteTextBox (https://wpfautocomplete.codeplex.com/).

I also have an on screen keyboard.

Whenever the user clicks on a textbox, the keyboard assigns this keybord to a property itself has and each click on a number adds that number to the textbox.

Some textbox must autocomplete and others must be the usual TextBox.

The problem here is that AutoCompleteTextBox doesn't inherits from TextBox, therefore I cannot make a unique "assign TextBox" for the keyboard. But both controls share the property "Text", which is the only I need to use.

public partial class Keypad : UserControl
{
    private AutoCompleteTextBox _controlAsignado; //this should be a generic class or an interface
    public Keypad() { }
    public void AsignarControl(AutoCompleteTextBox control)
    {
        _controlAsignado = control;
    }
    private void button_Click(object sender, RoutedEventArgs e)
    {
       Button button = sender as Button;
       switch (button.CommandParameter.ToString())
           {
           case "BACK":
               if (_controlAsignado.Text.Length > 0)
                            _controlAsignado.Text = _controlAsignado.Text.Remove(_controlAsignado.Text.Length - 1);
               break;
           default:
               _controlAsignado.Text += button.Content.ToString();
               break;
           }
    }

}

It would be awesome to have a "ITextBox" which both controls implement.

I know I can solve this issue using reflection but I want an elegant solution.

Horas Maquina and Orden Servicio must be "TextBox", the rest are AutoCompleteTextBox

Upvotes: 0

Views: 29

Answers (1)

Gábor Bakos
Gábor Bakos

Reputation: 9100

You can create a wrapper for both classes (preferably with an ITextBox interface too), so you can abstract away the differences and use runtime polymorphism:

interface ITextBox { string Text {get; set;} }
abstract class TextWrapper<T>: ITextBox where T: Control {
    T Wrapped;
    TextWrapper(T textControl) {
        this.Wrapped = textControl;
    }
    Text{ get{
        return Wrapped.Text;
    }
    set (string value) {
        Wrapped.Text = value;
    } 
}
class TextBoxWrapper: TextWrapper<TextBox> {}
class AutoCompleteTextBoxWrapper: TextWrapper<AutoCompleteTextBox> {}

Your code would look like this:

public partial class Keypad : UserControl
{
    private ITextBox _controlAsignado;
    public Keypad() { }
    public void AsignarControl(AutoCompleteTextBox control)
    {
        _controlAsignado = new AutoCompleteTextBoxWrapper(control);
    }
    private void button_Click(object sender, RoutedEventArgs e)
    {
       Button button = sender as Button;
       switch (button.CommandParameter.ToString())
           {
           case "BACK":
               if (_controlAsignado.Text.Length > 0)
                            _controlAsignado.Text = _controlAsignado.Text.Remove(_controlAsignado.Text.Length - 1);
               break;
           default:
               _controlAsignado.Text += button.Content.ToString();
               break;
           }
    }
}

(I have not tested the code, but I hope the general idea is what you were looking for.)

Upvotes: 2

Related Questions