0

I changed a data structure in my program. I want to have a List instead of a string. Now I have some trouble with the set function. this is the old code

    private string user;
    public string User
    {
        get { return user; }
        set
        {
            user = value;
            Notify("user");
        }
    }

I don't know how to manage user = value; This is my try:

    private List<string> user;
    public List<string> User
    {
        get { return user; }
        set
        {
            user.Add(value);
            Notify("user");
        }
    }

It throws an error because value is a List and can't be added to a list.

edit: sorry, it was a copy and paste mistake. I changed the old code with this edit.

So, what I want to do is binding a List to a column of a datagrid. The code above is part of an data class.

This is the code in the window.xaml

     <DataGridTextColumn Header = "user" Binding="{Binding Path=User, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MinWidth="50" />
tux007
  • 282
  • 1
  • 3
  • 17
  • 1
    You said you want to have List instead of a string. But you changed only the setter. What are you trying to achieve? – Marko Juvančič Jan 07 '17 at 16:10
  • oh, thanks for the hint. – tux007 Jan 07 '17 at 16:14
  • Side note: writable collection properties are problematic due to exactly the issue you've observed - semantic of setting it is unclear (see http://stackoverflow.com/questions/11349507/collection-properties-should-be-read-only-loophole). Consider to expose collection as read only interface like `IEnumerable`. – Alexei Levenkov Jan 07 '17 at 16:20

1 Answers1

2

In your example value would be of the same type as the Property, List<string>. So you can't use user.Add(value). Rather, you can write:

private List<string> user = new List<string>();
public List<string> User
{
    get { return user; }
    set
    {
        user = value;
        Notify("user");
    }
}

But if you are using Binding Users to a DataGrid, then it is better to use a collection that implements INotifyPropertyChanged like ObservableCollection<T>. That way, the view (DataGrid) is notified when an item is added/removed from the collection or the whole collection is refreshed.

private ObservableCollection<string> user = new ObservableCollection<string>();
public ObservableCollection<string> User
{
    get { return user; }
    set
    {
        user = value;
        Notify("user");
    }
}

For more information about binding an ObservableCollection to a DataGrid, check out this article.

And by the way, I think the property is better named Users not User because it's a list of users.

Clemens
  • 123,504
  • 12
  • 155
  • 268
Muhammad Azeez
  • 926
  • 8
  • 18
  • okay in this case I get an System.NullReferenceException in the constructor where user.Add("john doe "); is called. – tux007 Jan 07 '17 at 16:31
  • 1
    @tux007 it is not possible to properly answer your question because you did not explain what behavior of that property you want. Exposing r/w collection assumes that someone will set *whole collection* (at least initially) and will not try adding items before it is set. – Alexei Levenkov Jan 07 '17 at 16:34
  • @tux007 I edited the answer. To prevent this, you can initialize the backing field (`user`) like above, or you can initialize the property inside the constructor before using it. – Muhammad Azeez Jan 07 '17 at 16:36
  • Encryp0r - "should write" is very strong statement for this recommendation. "May write to avoid particular error" is more reasonable - next complain from OP will be setting the property wipes out previous results... There is really no good way to expose collection as writable property. What they should do is figure out what behavior is expected and code accordingly. – Alexei Levenkov Jan 07 '17 at 16:38
  • @AlexeiLevenkov thank you for the tip! – Muhammad Azeez Jan 07 '17 at 16:42
  • okay, I wrote some more explanations. – tux007 Jan 07 '17 at 16:44
  • @tux007 If you are binding to a datagrid, you might want to use an `ObservableCollection`. Because it notifies the view when an item is added/removed. So the view is always up-to-date. – Muhammad Azeez Jan 07 '17 at 16:50
  • I want to do it like shown in this thread: http://stackoverflow.com/questions/4978723/grid-table-in-wpf in the comments of the first answer – tux007 Jan 07 '17 at 16:54
  • @tux007 clarified a bit more, see if it helps – Muhammad Azeez Jan 07 '17 at 17:11
  • @Clemens wow, can't believe I had made the getter private, thanks dude! I meant to make the setter private, don't know how this happened. – Muhammad Azeez Jan 07 '17 at 17:58
  • 1
    hey guys, thanks a lot! – tux007 Jan 09 '17 at 00:10