开发者

WPF ComboBox Auto Select If Only 1 Item

开发者 https://www.devze.com 2023-01-04 09:37 出处:网络
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 t

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;


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>


Using only XAML:

<Style TargetType="{x:Type ComboBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource Self}}" Value="1">
            <Setter Property="SelectedIndex" Value="0" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Of course you may want to add a key to that style and base it on the default style for ComboBoxes to make it really useful, but I hope you get the idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消