如何创建一个从TextBox派生的对象?

时间:2012-10-02 10:51:24

标签: wpf

我尝试创建从TextBox派生的新类对象 - 如果TextBox中有字符 - 新对象将显示一些按钮,按下此按钮将能够删除此TextBox中的字符

如何在WPF中从控制中获得衍生物?

1 个答案:

答案 0 :(得分:3)

您可以使用文本框和按钮创建新的UserControl。将字符串属性绑定到文本框和按钮的visibility-property。然后创建一个转换器,将此字符串转换为可见性。现在,将按钮的Command属性绑定到设置字符串property = string.Empty。

的命令

一些提示:

如何使用转换器:

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

您的虚拟机如何:

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