Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ComboBox Auto Select If Only 1 Item

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;
like image 506
Michal Ciechan Avatar asked Dec 16 '22 23:12

Michal Ciechan


1 Answers

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>

like image 175
Samuel Jack Avatar answered Dec 24 '22 00:12

Samuel Jack