Yanshof
Yanshof

Reputation: 9926

How to create an object that derivative from TextBox ?

I trying to create new class object that derivative from TextBox - If there is chars in the TextBox - the new object will show some button and pressing on this button will be able to remove the chars in this TextBox

how can i make the derivative from control in WPF ?

Upvotes: 0

Views: 122

Answers (1)

Florian Gl
Florian Gl

Reputation: 6014

You could create a new UserControl with a textbox and a button. You bind a string property to the textbox and to the visibility-property of your button. Then you create a converter which converts this string to a visibility. Now you bind the Command-property of your button to a command which sets the string property = string.Empty.

A few hints:

How to use Converters:

<UserControl.Resources>
    <local:StringToVisibilityConverter x:Key="STV"></local:StringToVisibilityConverter>
</UserControl.Resources>
<Button Visibility="{Binding Path=MyText, Converter={StaticResource ResourceKey=STV}}" />

How your VM could look like:

public class MainViewModel:ViewModelBase
{
    private string _mytext;
    public string MyText
    {
        get
        {
            return _mytext;
        }
        set
        {
            _mytext = value;
            OnPropertyChanged("MyText");
        }
    }

    private RelayCommand<object> _clearTextCommand;
    public ICommand ClearTextCommand
    {
        get
        {
            if (_clearTextCommand == null)
            {
                _clearTextCommand = new RelayCommand<object>(o => ClearText(), o => CanClearText());
            }
            return _clearTextCommand;
        }
    }

    private void ClearText()
    {
        MyText = string.Empty;
    }

    private bool CanClearText()
    {
        return true;
    }
}

Upvotes: 3

Related Questions