Search code examples
c#wpfmvvmwaf

Converter - How many should I use? Are there other ways in WPF with MVVM?


I sometimes have labels where the content changes dynamically with the values of some objects. The strings are static though, but they need to be changed accordingly to my attributes.

An easy way would be, to implement a converter which takes my object and returns my desired string. This would result in many converters which only have one task and can't be used in different cases.

I could also change my title in my ViewModel -> Is this the better approach?


Solution

  • In your view model you can have the dynamic label property as such

    String DynamicLabel 
    {
        get
        {
             if ( this.x == 1 )
             {
                  return staticString1;
             }
             //etc etc 
        }
    }
    

    When ever the label needs changing you would just have to call

    OnPropertyChanged("DynamicLabel")
    

    and your xaml would look something along these lines

    <textblock text="{Binding Path = DynamicLabel , updateSourceTrigger = OnPropertyChanged}"/>