Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not clear on the latter part of your question. Partly it's because your naming convention is confusing; I'd expect a collection named <code>BItems</code> to contain objects of type <code>B</code>, not <code>A</code>.</p> <p>So I'm going to change your nomenclature a bit so that I don't get confused. Instead of <code>A</code>, I'll call the first class <code>User</code>, and instead of <code>B</code>, I'll call the second class <code>Group</code>. A <code>Group</code> contains a collection of <code>User</code> objects, named <code>Users</code>. The global collections look like this:</p> <pre><code>List&lt;User&gt; Users; List&lt;Group&gt; Groups; </code></pre> <p>It's easy to determine if a given <code>User</code> <code>u</code> is in any group:</p> <pre><code>return Groups.Where(g =&gt; g.Users.Contains(u)).Any(); </code></pre> <p>Easy, but computationally expensive if you have many groups and they contain many users. We'll get back to that in a second.</p> <p>Right away, I see that one of your questions has got a problem:</p> <blockquote> <p>How can I make that checking a checkbox inserts its item to the BItems-Collection and unchecking removes it?</p> </blockquote> <p>What should happen if I check an unchecked user? Which group (or groups, since more than one group can contain a user) should it be added to?</p> <p>Since you say that you want checked items to be "assigned to the B-Object", I'm going to assume that the UI is only looking at one group at a time - we'll call it the <code>SelectedGroup</code>. This is good, because <code>g.Users.Contains(u)</code> is much less expensive than the query I showed above.</p> <p>If this is so, what you need to do is wrap your <code>User</code> in a class that exposes an <code>IsChecked</code> property. I'd call this class <code>UserViewModel</code>, since that's what it is. The class needs three properties (at a minimum):</p> <pre><code>public User User { get; set; } public Group SelectedGroup { get; set; } public bool IsChecked { get { return SelectedGroup.Users.Contains(this.User); } set { if (value != IsChecked) { if (IsChecked) { SelectedGroup.Users.Remove(this.User); } else { SelectedGroup.Users.Add(this.User); } } } } </code></pre> <p>Your <code>ListView</code> is bound to an <code>ObservableCollection&lt;UserViewModel&gt;</code> named, say, <code>UserViewModels</code>. Whenever <code>SelectedGroup</code> is set, you need to rebuild this collection:</p> <pre><code>UserViewModels = new ObservableCollection&lt;UserViewModel&gt;( Users.Select(u =&gt; new UserViewModel { User=u, SelectedGroup=SelectedGroup })); </code></pre> <p>You could avoid rebuilding the collection by implementing <code>INotifyPropertyChanged</code> in the <code>UserViewModel</code> class, and having it raise <code>PropertyChanged</code> for the <code>IsChecked</code> property whenever <code>SelectedGroup</code> changes. </p> <p>Also, it would probably be responsible to include null-reference checking in the <code>IsChecked</code> property, so that the program doesn't throw an exception if <code>SelectedGroup</code> or <code>SelectedGroup.Users</code> is null.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload