Question

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?

Was it helpful?

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}"/>

OTHER TIPS

I think this may helpful to you, you can pass the parameters for different cases and check the conditions according to the parameter you passed in the converter.

Binding TestValue, Converter={StaticResource TestConverter}, ConverterParameter='Test'}

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (parameter != null && parameter.Equals("Test"))
        { //Do some operation 
        }
    }

By using parameter you can do different operations in a single converter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top