3

I know there are 100 questions about it, but I have not found an answer to this specific case.

I have an object that looks like this:

public string LogMessage { get; set; }
public DateTime Time { get; set; }        
public string[] Params { get; set; }

and I want to bind this object to datagrid (each object will be a row and each variable will be cell)

I tried to bind like this:

DataGridTextColumn Log = new DataGridTextColumn();
DataGridTextColumn Time = new DataGridTextColumn();
DataGridTextColumn Params = new DataGridTextColumn();
win.Table.Columns.Add(Time);
win.Table.Columns.Add(Log);
win.Table.Columns.Add(Params);
Time.Binding = new Binding("Time");
Log.Binding = new Binding("LogMessage");
Params.Binding = new Binding("Params");

But the result of the Params column is of course:"String[] Array".

I need to know if there is some option to manipulate the data after the bind. Something like:

new Binding("Params").ToJson();

Thanks!

4
  • 1
    remove all that code and create a proper ViewModel for this. then just bind the UI normally (via XAML) and not using procedural code. Commented Dec 17, 2013 at 15:47
  • I know this is the right way, but realy? there is no other options? Commented Dec 17, 2013 at 15:54
  • Because it's a small program and not really importance one.. so not optimal but easy solution would be enough Commented Dec 17, 2013 at 16:07
  • In a ListView GridView you could bind the array to a ComboBox or List. Or create a property that just returns the array as a single concatenated text. Commented Dec 17, 2013 at 17:02

1 Answer 1

2

There are probably several ways to do this. If you're able to change the class, then you could simply add a new property and bind to that:

public string[] ParamsString
{ 
    get { return string.Join(", ", Params); }
}

Alternatively, if you can't change the class, then you could create an IValueConverter that converts the array into the value you want. For example:

public class ArrayToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string[] input = value as string[];
        if (input != null && input.Length > 0)
            return string.Join(", ", input);
        return "";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Now you can add the converter to the binding like this:

Params.Binding = new Binding("Params") { Converter = new ArrayToStringConverter() };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.