- Select the first ComboBox item in the list on startup
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
- xmlns:loc ="clr-namespace:WpfApplication1"
- x:Class="WpfApplication1.MainWindow"
- x:Name="Window"
- Title="MainWindow"
- Width="640" Height="480">
- <StackPanel Orientation="Vertical">
- <StackPanel.Resources>
- <ComboBoxItem x:Key="toSelectInitially" Content="{Binding Path=ActiveItem, Mode=OneWay}"/>
- </StackPanel.Resources>
- <ComboBox SelectedIndex="0"
- Height="30">
- <ComboBox.ItemsSource>
- <x:Array Type="{x:Type ComboBoxItem}">
- <ComboBoxItem Content="{Binding Path=ActiveItem, Mode=OneWay}"/>
- <ComboBoxItem Content="AAA"/>
- <ComboBoxItem Content="BBB"/>
- </x:Array>
- </ComboBox.ItemsSource>
- </ComboBox>
- <ComboBox SelectedItem="{StaticResource ResourceKey=toSelectInitially}"
- Height="30" Loaded="ComboBox_Loaded">
- <ComboBox.ItemsSource>
- <x:Array Type="{x:Type ComboBoxItem}">
- <StaticResource ResourceKey="toSelectInitially"/>
- <ComboBoxItem Content="AAA"/>
- <ComboBoxItem Content="BBB"/>
- </x:Array>
- </ComboBox.ItemsSource>
- </ComboBox>
- <ComboBox SelectedValue="{Binding Path=ActiveItem, Mode=OneWay}"
- SelectedValuePath="Content"
- Height="30">
- <ComboBox.ItemsSource>
- <x:Array Type="{x:Type ComboBoxItem}">
- <ComboBoxItem Content="{Binding Path=ActiveItem, Mode=OneWay}"/>
- <ComboBoxItem Content="AAA"/>
- <ComboBoxItem Content="BBB"/>
- </x:Array>
- </ComboBox.ItemsSource>
- </ComboBox>
- </StackPanel>
- </Window>
- namespace WpfApplication1
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- this.InitializeComponent();
- this.DataContext = new VMSimple();
- }
- }
- }
- using System;
- using System.ComponentModel;
- namespace WpfApplication1
- {
- class VMSimple : INotifyPropertyChanged
- {
- public VMSimple()
- {
- ActiveItem = string.Concat("Active Item : ", Guid.NewGuid().ToString());
- }
- private string mActiveItem;
- public string ActiveItem
- {
- get { return mActiveItem; }
- private set
- {
- if (Equals(mActiveItem, value)) return;
- mActiveItem = value;
- OnPropertyChanged("ActiveItem");
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- private void OnPropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- }
- <ComboBox ItemsSource="{Binding Path=ComboItems}" SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"/>
- public class ViewModel : INotifyPropertyChanged
- {
- private List<string> m_ComboItems= new List<string>();
- private string m_SelectedItem;
- public ViewModel()
- {
- m_ComboItems.Add("AA");
- m_ComboItems.Add("BB");
- m_ComboItems.Add("CC");
- this.SelectedItem = m_ComboItems.First<string>();
- }
- public List<string> ComboItems
- {
- get { return m_ComboItems;}
- }
- public string SelectedItem
- {
- get { return m_SelectedItem; }
- set
- {
- m_SelectedItem = value;
- this.OnPropertyChanged("SelectedItem");
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string propertyName)
- {
- if (this.PropertyChanged != null)
- this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- <ComboBox IsSynchronizedWithCurrentItem="True"/>