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 864551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:29:49+00:00 2026-05-15T09:29:49+00:00

I have a combo box that I bind to an observable collection, which gets

  • 0

I have a combo box that I bind to an observable collection, which gets changed (according to company selected) and a large number of companies will have a single account (the items) therefore I want to know what the best way to make the ComboBox set the SelectedItem if there is only 1 item in the ItemsSource, otherwise leave it as null to ensure the user chooses an account.

The way I am doing this at the moment is by checking the account collection each time it is changed, and if it contains only one, setting the bound selected item property to the first item in the collection.

This seems long winded and I would need to implement it into each view model separately and write up to 5 lines of code for each combo box.

The following is the code I currently, but I was wondering if there would it be possible to achieve this by extending the ComboBox control? And if anyone has any ideas on how/where to start.

    public CompanyGermanPower FromCompany
    {
        get { return _fromCompany; }
        set
        {
            SetField(ref _fromCompany, value, () => FromCompany);
            if(value!= null)
            {
                FromTradeAccountList = new ObservableCollection<TradeAccount>(TradeAccountAdapter.GetTradeAccounts(_session, value.ID));
                if (Trade != null && FromTradeAccountList.Count == 1) Trade.TradeAccountFrom = FromTradeAccountList[0];
            }
        }
    } private CompanyGermanPower _fromCompany;
  • 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-05-15T09:29:49+00:00Added an answer on May 15, 2026 at 9:29 am

    It should be fairly straightforward to create an Attached Behaviour that does what you want. To detect when the Items collection in the ComboBox changed, you’d need to use the trick mentioned in this blog post.

    Update: Here’s my stab at it (you’ll need to add a reference to System.Windows.Interactivity to your project – you can get it from the Expression Blend SDK):

    using System.Windows.Interactivity;
    
    public class SelectFirstItemComboBoxBehavior : Behavior<ComboBox>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
    
            (AssociatedObject.Items as INotifyCollectionChanged).CollectionChanged += HandleCollectionChanged;
        }
    
        protected override void OnDetaching()
        {
            base.OnDetaching();
            (AssociatedObject.Items as INotifyCollectionChanged).CollectionChanged -= HandleCollectionChanged;
        }
    
        private void HandleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (AssociatedObject.Items.Count == 1)
            {
                AssociatedObject.SelectedItem = AssociatedObject.Items.Cast<object>().First();
            }
        }
    }
    

    Here’s how you use it:

    <Window x:Class="ComboBoxSelectFirstBehavior.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ComboBoxSelectFirstBehavior"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <x:Array x:Key="MyObjects" Type="{x:Type local:MyType}">
                <local:MyType Name="WithChildren">
                    <local:MyType.Children>
                        <local:MyTypeCollection>
                            <local:MyType Name="Child1"/>
                            <local:MyType Name="Child2"/>
                            <local:MyType Name="Child3"/>
                        </local:MyTypeCollection>
                    </local:MyType.Children>
                </local:MyType>
                <local:MyType Name="WithOneChild">
                    <local:MyType.Children>
                        <local:MyTypeCollection>
                            <local:MyType Name="Child1"/>
                        </local:MyTypeCollection>
                    </local:MyType.Children>
                </local:MyType>
                <local:MyType Name="WithoutChildren">
                        <local:MyType.Children>
                            <local:MyTypeCollection>
                            </local:MyTypeCollection>
                        </local:MyType.Children>
                    </local:MyType>
            </x:Array>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ComboBox x:Name="FirstCombo" Grid.Row="0" ItemsSource="{StaticResource MyObjects}" DisplayMemberPath="Name"/>
    
        <ComboBox Grid.Row="1" ItemsSource="{Binding ElementName=FirstCombo, Path=SelectedItem.Children}" DisplayMemberPath="Name">
            <i:Interaction.Behaviors>
                <local:SelectFirstItemComboBoxBehavior/>
            </i:Interaction.Behaviors>
        </ComboBox>
    </Grid>
    

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

Sidebar

Related Questions

I have a function that presents the user a combo-box. def select_interface(interfaces) list_box :items
I have a combo box on a WinForms app in which an item may
I have a combo box in Silverlight. It has a collection of values built
I have a Flex ComboBox that gets populated by a dataprovider all is well...
I have a radajaxpanel that is populated with the selected item from a combobox
I have a combo box with the following DataTemplate: <DataTemplate x:Key=ComboBoxDataTemplate> <StackPanel> <TextBlock Text={Binding
I have a ComboBox that I set up like this: this.cmbCustomerJob.DisplayMember = display; this.cmbCustomerJob.AutoCompleteMode
I want to have a select-only ComboBox that provides a list of items for
I have a combobox on my form that is bound to a generic list
I have a listview with a DataTemplate that has a ComboBox. I want the

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.