Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It should be fairly straightforward to create an <a href="http://blog.kirupa.com/?p=374" rel="noreferrer">Attached Behaviour</a> 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 <a href="http://www.wpfmentor.com/2008/11/notification-when-items-are-added-or.html" rel="noreferrer">blog post</a>.</p> <p><strong>Update</strong>: 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):</p> <pre><code>using System.Windows.Interactivity; public class SelectFirstItemComboBoxBehavior : Behavior&lt;ComboBox&gt; { 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&lt;object&gt;().First(); } } } </code></pre> <p>Here's how you use it:</p> <pre><code>&lt;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"&gt; &lt;Grid&gt; &lt;Grid.Resources&gt; &lt;x:Array x:Key="MyObjects" Type="{x:Type local:MyType}"&gt; &lt;local:MyType Name="WithChildren"&gt; &lt;local:MyType.Children&gt; &lt;local:MyTypeCollection&gt; &lt;local:MyType Name="Child1"/&gt; &lt;local:MyType Name="Child2"/&gt; &lt;local:MyType Name="Child3"/&gt; &lt;/local:MyTypeCollection&gt; &lt;/local:MyType.Children&gt; &lt;/local:MyType&gt; &lt;local:MyType Name="WithOneChild"&gt; &lt;local:MyType.Children&gt; &lt;local:MyTypeCollection&gt; &lt;local:MyType Name="Child1"/&gt; &lt;/local:MyTypeCollection&gt; &lt;/local:MyType.Children&gt; &lt;/local:MyType&gt; &lt;local:MyType Name="WithoutChildren"&gt; &lt;local:MyType.Children&gt; &lt;local:MyTypeCollection&gt; &lt;/local:MyTypeCollection&gt; &lt;/local:MyType.Children&gt; &lt;/local:MyType&gt; &lt;/x:Array&gt; &lt;/Grid.Resources&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition/&gt; &lt;RowDefinition/&gt; &lt;/Grid.RowDefinitions&gt; &lt;ComboBox x:Name="FirstCombo" Grid.Row="0" ItemsSource="{StaticResource MyObjects}" DisplayMemberPath="Name"/&gt; &lt;ComboBox Grid.Row="1" ItemsSource="{Binding ElementName=FirstCombo, Path=SelectedItem.Children}" DisplayMemberPath="Name"&gt; &lt;i:Interaction.Behaviors&gt; &lt;local:SelectFirstItemComboBoxBehavior/&gt; &lt;/i:Interaction.Behaviors&gt; &lt;/ComboBox&gt; &lt;/Grid&gt; </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload