2
votes

Sorry for my bad english.

On XAML I am binding an Object of my own class "Parameter" via a converter in Datagridtextcolumn.

XAML

<DataGridTextColumn Header="Min" Width="50" Binding="{Binding Path=., Converter={StaticResource MinMaxValueConverter}}">

C#

public class MinMaxValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Parameter p = (Parameter)value;
        if (p.Typ == Parameter.ParameterTyp.k1000) return p.LowerBorder;
        else return p.LowerBorder.ToString("X4") + "h";

    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        ...
        return parameter;
    }
}

I dont know how to write the CovertBack. The ConvertBack is reputed to replace a value in a variable of the Object "Paramter" without changing the other Variables or delete. But how can i do this without a reference to this object? I tried to bind the Object to the ConverterParameter, but it doesn't work.

I need the Value of "Parameter.Typ" to know how to Convert it Back. One idea was to make a Multibinding with "Parameter.Typ" and "Parameter.LowerBurder", but i can't convert Back without knowing what Typ ist in the actual row.

Makes me aware of all my mistakes!!!

1

1 Answers

1
votes

In general: the ConvertBack() method is required to accept the current value stored in the binding target, and convert it back to the type needed for the binding source.

Unfortunately, you have not provided a good Minimal, Complete, and Verifiable example that shows clearly what you are doing. Unless you do, it will be impossible to provide an answer that is guaranteed to address your specific concern. But from the information you've provided, some basic observations and suggestions can be made:

  1. First, your Convert() method appears to have two basic modes: if the source value has Typ property having the value k1000, then you return the LowerBorder property value directly. Otherwise, you format the LowerBorder property value to a string (apparently a hexadecimal value) and return that.
  2. Since you are formatting in some cases the LowerBorder value as a hexadecimal string, it seems reasonable to assume this property has an integral type, e.g. int.
  3. To convert back, you'll need to be able to differentiate these two cases, and reverse the conversion. The most obvious difference in the target value that would result from the conversion you've implemented is the trailing h character that would occur in one case, but not the other. So, let's use that.

In that case, you might be able to write your ConvertFrom() method as something like this:

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    string text = (string)value;

    return text[text.Length - 1] == 'h' ?
        return FromHexString(text) :
        return FromInt32(text);
}

where:

Parameter FromHexString(string text)
{
    Parameter parameter = new Parameter();

    parameter.Typ = Parameter.ParamaterTyp.k1001;
    parameter.LowerBorder = int.Parse(
        text.Substring(0, text.Length - 1), NumberStyles.AllowHexSpecifier);
}


Parameter FromInt32(string text)
{
    Parameter parameter = new Parameter();

    parameter.Typ = Parameter.ParamaterTyp.k1000;
    parameter.LowerBorder = int.Parse(text);
}

Note: I have no idea what the Typ value should be when the value is hex, so I just made up a new value name of k1001 for the purpose of the example. Indeed, if more than one Typ value could be possible, you're going to have to make a judgment call as to what Typ value to use, or you'll have to change the source-to-target conversion so that information is preserved (e.g. use a different trailing character depending on the Typ value).

If the above is not sufficient for you to understand what the ConvertBack() method is required to do, and to implement that method to suit your needs, please improve your question so that it includes a good code example, and specific details as to what the code should do in each case.