maxp
maxp

Reputation: 25171

XAML only Multibinding / Trigger in WPF?

I have read-only access to a view-model, connected to my XAML view.

In Pseudo-ish code, it looks something like:

public class HelloWorld:INotifyPropertyChanged{
  private int _counter;
  public int Counter {get{return ++_counter;}}
  public bool Foo {get{...}set{...InvokeINotifyPropertyChanged("Foo");}}
}

I have a TextBlock with it's Text property bound to Counter, but would like it's value to be re-evaluated when Foo is changed / set, in markup only.

I can solve this problem, by binding TextBlock's Text property to a MultiBinding and including both Counter and Foo as bindings, followed by creating a Converter that implements IMultiValueConverter, but this seems a bit like overkill.

Is there a nicer way?

Upvotes: 0

Views: 181

Answers (1)

Rodrigo Vedovato
Rodrigo Vedovato

Reputation: 1008

When you say read-only, you mean that you can't even access the ViewModel source code? In this case, i would recommend something like this:

public TestClass()
{
    _viewModel.PropertyChanged += OnPropertyChanged;
}

private void OnPropertyChanged(object sender, PropertyChangedEventArgs args)
{
    if (args.PropertyName == "Foo")
    {
        _textBlock.GetBindingExpression(TextBlock.TextProperty).UpdateTarget();
    }
}

But if you have access to the View-Model code, you can simply call InvokeINotifyPropertyChanged("Counter") on Foo setter.

Upvotes: 1

Related Questions