27

Funny, how sometimes the simple things bite me in the behind.

I can bind a DataGrid nicely to a Collection of some arbitrary class, using a DataGridTextColumn like this:

// bound to List<Class>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>

Now I want to bind a DataGrid to a simple Collection of String. So, since there is no property "Name" or something like that to bind to how do I write the binding:

// bound to List<String>
<DataGridTextColumn Header="Name" Binding="{Binding ???}"/>

String has no Property "Value" or something like that. And if I just write {Binding } I'll end up with a one-way-binding, unable to write changes back to the Collection.

Thinking about it, I think it is not possible to bind to a collection<String>, so I do need to wrap my string into a class?
Or is there another way?

1 Answer 1

40

You can make it run with the following Binding:

Binding="{Binding Path=.}

But it wont solve your problem, because strings are reference typed that are immutable, meaning you cannot change the string reference you have bound to your user interface.

So your thoughts are correct, you will need to wrap these strings in objects, use the path property of Binding and feed these objects to your DataGrid.

public class StringWrapper
{
    public string Value { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

But why is the wrapper working? We are still binding to a string right?
No.. You're binding to an object that contains a string. Thats the difference
I understand now. Binding cannot change the bound reference. It can only change properties. It makes sense now. Thanks a lot.

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.