-1

I have an iNotifyChange property bound to a header in my XAML. What I would like is from code behind to be able to update an int value but have the string returned to the XAML. I.e. code updates property to 6, XAML updates to "Warnings: 6". The problem is that a property's type can't be different to its return type. How should I modify the below to make this work?

<Expander Header="{Binding Path=DATErrorsHeader, UpdateSourceTrigger=PropertyChanged}">

private int _overallError;
public string ErrorsWarningsHeader
{
    get { return "Warnings: " + _overallError.ToString(); }
    set
    {
        int.TryParse(value, out _overallError);
        NotifyPropertyChanged();
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
    var handler = PropertyChanged;
    if (handler != null)
        handler(this, new PropertyChangedEventArgs(propertyName));
}

In code I'm basically doing;

viewModel.ErrorsWarningsHeader = "6";

I would rather this be an int so that I can add onto the property's current value.

windowsgm
  • 1,566
  • 4
  • 23
  • 55
  • _"but have the string returned to the XAML"_ -- why? WPF will convert an `int` to `string` for you, if necessary. So what's the problem you are having? – Peter Duniho Aug 11 '17 at 22:39
  • @PeterDuniho So I want the functionality of being able to say myProperty = myProperty + 1 (i.e. an int) in code behind but with the returned value to XAML being "Some Text: " + myProperty (i.e. a string). – windowsgm Aug 15 '17 at 09:51

2 Answers2

1

You could add another property of a different datatype that is backed by the same field in your class.

public int ErrorsWarningsHeaderInt
{
    get { return _overallError; }
    set
    {
        // TODO: Validation of 'value'
        _overallError = value;
        NotifyPropertyChanged(nameof(ErrorsWarningsHeader));
    }
}
Michael
  • 1,931
  • 2
  • 8
  • 22
  • Thanks for that, one last follow on question; lets say I have a bunch of these properties all wired up to their respective elements. But I also have one other Header that is designed to show the total of all the other values, at what point should I add each value to this property? In the NotifyPropertyChanged? – windowsgm Aug 11 '17 at 16:28
  • I am not really sure that I understand your comment... Since this property _shares the same backing field_ as the one bound in the UI there should be no changes to your code besides the fact that you have to access the `ErrorsWarningHeaderInt` in code/code-behing and `ErrorsWarningHeader` in your XAML – Michael Aug 12 '17 at 08:31
1

This is a good case to write your own Converter. You can create a class that inherits from IValueConverter and implement the Convert method.

Once you have that, import than namespace to your xaml and you could to sth like below in your xaml:

<TextBlock Text = "{Binding WarningNumber//your int prop name, 
Converter = {StaticResource WarningNumberToDisplayMsgConverter //name of your converter type}}"
haku
  • 4,105
  • 7
  • 38
  • 63
  • Seems like a good suggestion. However, I'm actually applying this to about 10 XAML elements. The string that my converter is tacking on the start of the display name ("Warnings: " in this example) will be different for each element which means I'd need a different converter for each element (because afaik you can't pass params back from XAML). Unless I do some finicky figuring out the element that called the converter then select the string maybe based on the element name...?? Thoughts? – windowsgm Aug 11 '17 at 16:12
  • 1
    The third parameter of the Convert method takes object param to which you could send a value from your xaml. Depending on your control send the prefix you want as param, do your magic in the method, and return the display string. Its been a while since I have done xaml but i think that should help. – haku Aug 11 '17 at 16:46