简体   繁体   中英

WPF execute ICommand using attached properties and behaviours

So I'm learning about attached properties and attached behaviours. I have a TextBox and everytime the text is changed, I want to execute some ICommand SomeCommand in my ViewModel which I would bind in the view like this:

<TextBox ... TextUpdateCommand="{Binding Path=SomeCommand}" MonitorTextBoxProperty=true />

MonitorTextBoxProperty is just a property that listens to TextChanged event and then executes ICommand SomeCommand when TextChanged is fired. The ICommand SomeCommand that is executed should come from TextUpdateCommandProperty . How do I link this ICommand SomeCommand to execute it from OnTextBoxTextChanged ?

Code:

//Property and behaviour for TextChanged event

public static int GetMonitorTextBoxProperty(DependencyObject obj)
{
    return (int)obj.GetValue(MonitorTextBoxProperty);
}

public static void SetMonitorTextBoxProperty(DependencyObject obj, int value)
{
    obj.SetValue(MonitorTextBoxProperty, value);
}

public static readonly DependencyProperty MonitorTextBoxProperty =
     DependencyProperty.RegisterAttached("MonitorTextBox", typeof(bool), typeof(this), new PropertyMetadata(false, OnMonitorTextBoxChanged));

private static void OnMonitorTextBoxChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var textBox = (d as TextBox);
    if (textBox == null) return;

    textBox.TextChanged -= OnTextBoxTextChanged;

    if ((bool)e.NewValue)
    {
        textBox.TextChanged += OnTextBoxTextChanged;
    }
}

private static void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
 {
    // Execute ICommand by command.Execute()
 }

// Property for attaching ICommand that is executed on TextChanged

public static int GetTextUpdateCommandProperty(DependencyObject obj)
{
    return (int)obj.GetValue(TextUpdateCommandProperty);
}

public static void SetTextUpdateCommandProperty(DependencyObject obj, int value)
{
    obj.SetValue(TextUpdateCommandProperty, value);
}

public static readonly DependencyProperty TextUpdateCommandProperty =
    DependencyProperty.RegisterAttached("TextUpdateCommand", typeof(ICommand), typeof(this), new PropertyMetadata(null));

Just get the current value of the TextBox 's TextUpdateCommand property using the attached property's get accessor. Try this:

private static void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
    var textBox = sender as TextBox;
    var command = GetTextUpdateCommandProperty(textBox);
    if (command != null)
         command.Execute(null);
}

You should also change the type of the accessors:

public static ICommand GetTextUpdateCommandProperty(DependencyObject obj)
{
    return (ICommand)obj.GetValue(TextUpdateCommandProperty);
}

public static void SetTextUpdateCommandProperty(DependencyObject obj, ICommand value)
{
    obj.SetValue(TextUpdateCommandProperty, value);
}

And typeof(this) should the typeof(TheClassName) in the call to DependencyProperty.RegisterAttached .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM