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

  • SEARCH
  • Home
  • 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 8367889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:15:52+00:00 2026-06-09T13:15:52+00:00

I have a window with a combobox. This comboboxhas 5 ComboboxItems. I binding the

  • 0

I have a window with a combobox. This comboboxhas 5 ComboboxItems.

I binding the SelectedItem (combobox) to the ComboBoxSelectedIndex Property in my code behind file.

In the example I want that it is not possible to select the items 4 and 5.

But i can select the item 4 and 5. What’s wrong?

xaml code:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStartupLocation="CenterScreen"
        Height="350"
        Width="500">
    <StackPanel VerticalAlignment="Center">
        <ComboBox SelectedIndex="{Binding Path=ComboBoxSelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <ComboBoxItem>Item 1</ComboBoxItem>
            <ComboBoxItem>Item 2</ComboBoxItem>
            <ComboBoxItem>Item 3</ComboBoxItem>
            <ComboBoxItem>Item 4</ComboBoxItem>
            <ComboBoxItem>Item 5</ComboBoxItem>
        </ComboBox>     
    </StackPanel>
</Window>

codebehind file:

namespace WpfApplication1
{
    public partial class MainWindow : INotifyPropertyChanged
    {
        private int _selectedIndex;

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        public int ComboBoxSelectedIndex
        {
            get { return _selectedIndex; }
            set
            {
                if (value < 3)
                {
                    _selectedIndex = value;
                }
                OnPropertyChanged("ComboBoxSelectedIndex");
                Trace.WriteLine(ComboBoxSelectedIndex);
            }
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}

(I am aware that I can solve this problem with the property Is Enabled. But I don’t what that here)

  • 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-09T13:15:53+00:00Added an answer on June 9, 2026 at 1:15 pm

    Create your custom combobox to implement this

    <StackPanel Orientation="Vertical">
        <wpfProj:ExtendedCombobBox 
           SelectedIndex="{Binding Path=ComboBoxSelectedIndex, 
                                  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
           MaxSelectedIndex="{Binding Path=MaxSelectedIndex}">
            <ComboBoxItem>Item 1</ComboBoxItem>
            <ComboBoxItem>Item 2</ComboBoxItem>
            <ComboBoxItem>Item 3</ComboBoxItem>
            <ComboBoxItem>Item 4</ComboBoxItem>
            <ComboBoxItem>Item 5</ComboBoxItem>
        </wpfProj:ExtendedCombobBox>
    </StackPanel>
    

    And code the custom combobox

    public class ExtendedCombobBox:ComboBox
    {
        public static readonly DependencyProperty MaxSelectedIndexProperty =
            DependencyProperty.Register("MaxSelectedIndex", typeof (int), typeof (ExtendedCombobBox), new PropertyMetadata(default(int)));
    
        public int MaxSelectedIndex
        {
            get { return (int) GetValue(MaxSelectedIndexProperty); }
            set { SetValue(MaxSelectedIndexProperty, value); }
        }
    
        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            if (Items.IndexOf(e.AddedItems[0]) > MaxSelectedIndex)
                e.Handled = true;
            else
                base.OnSelectionChanged(e);
        }
    }
    

    UPD1.
    Or you can just use standard one and subscribe to the SelectionChanged event. But I’d prefer to use custombobox.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a combobox (ItemsSource={Binding Path=AvailableDrives}). The AvailableDrives property is defined like this: public
I have a combobox in WPF like this: <ComboBox Text=Select Language... IsEditable=True IsReadOnly=True ItemsSource={Binding
I have a WPF application. In one window there is a combobox..and I want
I have simple window. This is what happens when I click ComboBox: List appears
I have a combobox in a WPF Window, it is filled with usernames from
I have one combobox on the window form and I have one method which
I have a System.Windows.Forms.ComboBox on a form and I want to capture the event
I have a window I've created using UiBinder. I'm followings Sencha's HelloWorldUiBinder example and
I have a window containing three NSTextField s, like this: +------------------------+ | +--------------------+ |
I have trivial window: this.window = Ext.widget('window', { title: 'Find', closeAction: 'hide', width: 300,

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.