Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Single Selection Across Multiple ItemsControls
    primarykey
    data
    text
    <p>Part of my app has a month-view calendar interface, but I'm having trouble with item selection. The interface is set up so that each of the days in the view contains a <code>ListBox</code> of items, much like the month view in Outlook. The problem I'm experiencing is that I need to maintain a single item selection across all of the <code>ListBox</code>es.</p> <p>Below is a sample that should adequately describe my situation. I need to maintain a single selection between both <code>ListBox</code>es.</p> <pre><code>&lt;Window x:Class="StackOverflow.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition /&gt; &lt;ColumnDefinition /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;ListBox ItemsSource="{Binding Numbers}" SelectedItem="{Binding SelectedObject"/&gt; &lt;ListBox Grid.Column="1" ItemsSource="{Binding Dates}" SelectedItem="{Binding SelectedObject"/&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>And the view model for the window:</p> <pre><code>class MainWindowViewModel : DependencyObject { public static readonly DependencyProperty SelectedObjectProperty = DependencyProperty.Register("SelectedObject", typeof(object), typeof(MainWindowViewModel), new UIPropertyMetadata(null)); public ObservableCollection&lt;int&gt; Numbers { get; set; } public ObservableCollection&lt;DateTime&gt; Dates { get; set; } public object SelectedObject { get { return GetValue(SelectedObjectProperty); } set { SetValue(SelectedObjectProperty, value); } } } </code></pre> <p>In this primitive example, I would expect that when the SelectedObject property of my view model gets set to an item that's not in one <code>ListBox</code>, the selection would be removed in that <code>ListBox</code>, but that doesn't happen. I understand that I can simply name each ListBox, and hook into the SelectionChanged event. I'd prefer to not have to do that with an entire month-view calendar. There <em>has</em> to be a better way.</p> <p>In a previous iteration of the app, I was able to create a SelectionManager static class with an attached property that was used to maintain selection. However, I can't use this now as the classes I'm using for my items are not <code>DependencyObject</code>s, and I'd really prefer not to have to create <code>DependencyObject</code> wrapper classes as this will considerably complicate my architecture.</p> <p>Thanks.</p> <p><strong>EDIT 1:</strong> Added a view model class as requested.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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