Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8655957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:09:42+00:00 2026-06-12T15:09:42+00:00

I am using MVVM and am displaying two listboxes on one window. I am

  • 0

I am using MVVM and am displaying two listboxes on one window. I am binding from both of those listboxes simultaneously to different fields call them A and B. A and B are then both modifying C. To make this work, I want to only have one item from the two listboxes IsSelected at once, so that A does not override C when B IsSelected. How can I restrict this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:09:43+00:00Added an answer on June 12, 2026 at 3:09 pm

    I can think of a couple of ways to do this.

    One way is you could bind the ListBox.SelectedIndex of your 2 ListBoxes to change-notifying ViewModel properties.

    For example in your View:

    <ListBox SelectedIndex="{Binding SelectedIndexA}">
         <ListBoxItem Content="Item 1"/>
         <ListBoxItem Content="Item 2"/>
    </ListBox>
    <ListBox SelectedIndex="{Binding SelectedIndexB}">
         <ListBoxItem Content="Item 1"/>
         <ListBoxItem Content="Item 2"/>
    </ListBox>
    

    And in your ViewModel:

    public int SelectedIndexA
    {
        get { return _selectedIndexA; }
        set
        {
            _selectedIndexA = value;
            _selectedIndexB = -1;
            OnPropertyChanged("SelectedIndexB");
        }
    }
    
    public int SelectedIndexB
    {
        get { return _selectedIndexB; }
        set
        {
            _selectedIndexB = value;
            _selectedIndexA = -1;
            OnPropertyChanged("SelectedIndexA");
        }
    }
    

    Another way would be with an attached property like ‘GroupName’ where you can group Selectors (ListBox inherits from Selector) to ensure only one Selector in the group has a selected item at any one time.

    For example:

    public static class SingleSelectionGroup
    {
        public static readonly DependencyProperty GroupNameProperty =
            DependencyProperty.RegisterAttached("GroupName", typeof(string), typeof(SingleSelectionGroup),
                                                new UIPropertyMetadata(OnGroupNameChanged));
    
        public static string GetGroupname(Selector selector)
        {
            return (string) selector.GetValue(GroupNameProperty);
        }
    
        public static void SetGroupName(Selector selector, string value)
        {
            selector.SetValue(GroupNameProperty, value);
        }
    
        private static void OnGroupNameChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var selector = (Selector) dependencyObject;
    
            if (e.OldValue != null)
                selector.SelectionChanged -= SelectorOnSelectionChanged;
            if (e.NewValue != null)
                selector.SelectionChanged += SelectorOnSelectionChanged;
        }
    
        private static void SelectorOnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
                return;
    
            var selector = (Selector) sender;
            var groupName = (string) selector.GetValue(GroupNameProperty);
            var groupSelectors = GetGroupSelectors(selector, groupName);
    
            foreach (var groupSelector in groupSelectors.Where(gs => !gs.Equals(sender)))
            {
                groupSelector.SelectedIndex = -1;
            }
        }
    
        private static IEnumerable<Selector> GetGroupSelectors(DependencyObject selector, string groupName)
        {
            var selectors = new Collection<Selector>();
            var parent = GetParent(selector);
            GetGroupSelectors(parent, selectors, groupName);
            return selectors;
        }
    
        private static DependencyObject GetParent(DependencyObject depObj)
        {
            var parent = VisualTreeHelper.GetParent(depObj);
            return parent == null ? depObj : GetParent(parent);
        }
    
        private static void GetGroupSelectors(DependencyObject parent, Collection<Selector> selectors, string groupName)
        {
            var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                var selector = child as Selector;
                if (selector != null && (string) selector.GetValue(GroupNameProperty) == groupName)
                    selectors.Add(selector);
    
                GetGroupSelectors(child, selectors, groupName);
            }
        }
    }
    

    And in your View:

    <ListBox my:SingleSelectionGroup.GroupName="Group A">
        <ListBoxItem Content="Item 1 (Group A)"/>
        <ListBoxItem Content="Item 2 (Group A)"/>
    </ListBox>
    <ListBox my:SingleSelectionGroup.GroupName="Group A">
        <ListBoxItem Content="Item 1 (Group A)"/>
        <ListBoxItem Content="Item 2 (Group A)"/>
    </ListBox>
    
    <ListBox my:SingleSelectionGroup.GroupName="Group B">
        <ListBoxItem Content="Item 1 (Group B)"/>
        <ListBoxItem Content="Item 2 (Group B)"/>
    </ListBox>
    <ListBox my:SingleSelectionGroup.GroupName="Group B">
        <ListBoxItem Content="Item 1 (Group B)"/>
        <ListBoxItem Content="Item 2 (Group B)"/>
    </ListBox>
    

    If you have to click an item twice before it is highlighted you can use a quick workaround like this:

    <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <EventTrigger RoutedEvent="GotKeyboardFocus">
                <BeginStoryboard>
                    <Storyboard>
                        <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(ListBoxItem.IsSelected)">
                            <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True" />
                        </BooleanAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MVVM, from one I know the commnication for Data is View
I am using MVVM pattern and I have one DataGrid with a column which
I'm using MVVM in a WPF app. I'm very new to both. Let me
Using MVVM and EF...I have a datagrid binding to a View Model (using ObservableCollection).
I'm using MVVM. <ItemsControl ItemsSource={Binding AllIcons} Tag={Binding}> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel> <Label HorizontalAlignment=Right>x</Label> <Image Source={Binding
Using MVVM Light, I have two WPF applications that reference a common Views library.
Using MVVM, one type of ViewModels include the Model they represnt as a Field.
I'm using MVVM, in case it makes a difference. My MainWindowViewModel has two DependencyProperties,
I am using Mvvm-Light and have been ignoring how binding in XAML really works
I am using MVVM in my application and binding combobox to my collection. However

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.