Note that there are some explanatory texts on larger screens.

plurals
  1. PODataBind Listbox SubSelection in WPF
    text
    copied!<p>Okay, I might be asking an age old question, but I did not get my scenario described in any of them.</p> <p>I have an Oject which can contain several child Objects. Eg. A Project obejct can have several Resource objects. I have an ObservaleCollection with super set of child obejcts (in my case Resource objects). I also have another ObservableCollection in Project object containing existing childs.</p> <p>What is the best way to present this to user in WPF windows application? I also need to provide a way for them to chnage the mapping as well.</p> <p>My initial idea was to use classic Double List approach, with two listboxes, but I am not sure how easy it would be to manipulate view layer alone.</p> <pre><code>[Resoure Collection] [Resoure Collection in a Project] -------------------- --------------------------------- |Resource 1 | &gt; |Resource 3 | |Resource 2 | &gt;&gt; |Resource 4 | |Resource 5 | &lt; | | |Resource 6 | &lt;&lt; | | |Resource 7 | | | </code></pre> <p>I need similar UI for 4 more similar mappings of different objects. I tried to move this to a user control, but looks like I can't have Generic collection (private ObservableCollection) in an UserControl.</p> <p>Any ideas from experience members?</p> <p>/<strong><em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>****</strong>/ Edit: THis is what I got so far, note that I am going with UserControl since I need the same UI in multiple screens, and I feel UserCOntrol will give me more manageable code.</p> <p>XAML for the user control</p> <pre><code>&lt;UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="TimeTracker.ItemsSelectionLists" x:Name="ItemsSelectionControl"&gt; &lt;Grid x:Name="LayoutRoot"&gt; &lt;Grid Background="#FFF9FDFD" Margin="0,0,0,0"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock x:Name="SourceHeading" Grid.Column="0" Margin="8,8,0,0" TextWrapping="Wrap" Text="Whole Team" VerticalAlignment="Top" /&gt; &lt;ListBox x:Name="SourceItemsList" Grid.Column="0" Margin="8,30,8,8" MinWidth="150" SelectionMode="Multiple" ItemsSource="{Binding Path=Collection1}"/&gt; &lt;StackPanel Grid.Column="1" Margin="0" Orientation="Vertical" VerticalAlignment="Center"&gt; &lt;Button Content="&amp;gt;" Height="25" Width="25" /&gt; &lt;Button Content="&amp;gt;&amp;gt;" Height="25" Width="25" /&gt; &lt;Button Content="&amp;lt;" Height="25" Width="25" /&gt; &lt;Button Content="&amp;lt;&amp;lt;" Height="25" Width="25" /&gt; &lt;/StackPanel&gt; &lt;TextBlock x:Name="TargetHeading" Grid.Column="2" Margin="8,8,8,0" TextWrapping="Wrap" Text="Current Team" VerticalAlignment="Top" /&gt; &lt;ListBox x:Name="SelectedItemsList" Grid.Column="2" Margin="8,30,8,8" MinWidth="150" ItemsSource="{Binding Path=Collection2}"/&gt; &lt;/Grid&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>Code:</p> <pre><code>/// &lt;summary&gt; /// Interaction logic for ItemsSelectionLists.xaml /// &lt;/summary&gt; public partial class ItemsSelectionLists: UserControl { [Bindable(true)] internal ObservableCollection&lt;TrackerItem&gt; SourceList { get { return _vm.Collection1; } set { _vm.Collection1 = value; } } private readonly ViewModel _vm; public ItemsSelectionLists() { this.InitializeComponent(); _vm = new ViewModel(); this.DataContext = _vm; } } public class ViewModel : INotifyPropertyChanged { #region Properties private ObservableCollection&lt;TrackerItem&gt; _collection1; /// &lt;summary&gt; /// This is the first collection. /// &lt;/summary&gt; internal ObservableCollection&lt;TrackerItem&gt; Collection1 { get { return _collection1; } set { if (value != _collection1) { _collection1 = value; NotifyPropertyChanged("Collection1"); } } } private ObservableCollection&lt;TrackerItem&gt; _collection2; /// &lt;summary&gt; /// This is the second collection. /// &lt;/summary&gt; internal ObservableCollection&lt;TrackerItem&gt; Collection2 { get { return _collection2; } set { if (value != _collection2) { _collection2 = value; NotifyPropertyChanged("Collection2"); } } } #endregion #region Constructors /// &lt;summary&gt; /// Default constructor. /// &lt;/summary&gt; public ViewModel() { // Create initial collections. // Populate first collection with sample data _collection1 = new ObservableCollection&lt;TrackerItem&gt;(); // Seconf collection is empty _collection2 = new ObservableCollection&lt;TrackerItem&gt;(); } #endregion #region INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } #endregion } </code></pre> <p>Main Window</p> <pre><code>&lt;TabItem Header="Resource Allocation"&gt; &lt;local:ItemsSelectionLists x:Name="ResourceSelection"/&gt; &lt;/TabItem&gt; </code></pre> <p>Code</p> <pre><code>ResourceSelection.SourceList = MainObject.Resources; //error CS0029: Cannot implicitly convert type 'System.Collections.ObjectModel.ObservableCollection&lt;TimeTracker.Resource&gt;' to 'System.Collections.ObjectModel.ObservableCollection&lt;TimeTracker.TrackerItem&gt;' </code></pre>
 

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