Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 3.99 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Select the first ComboBox item in the list on startup
  2. <Window
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
  6. xmlns:loc ="clr-namespace:WpfApplication1"
  7. x:Class="WpfApplication1.MainWindow"
  8. x:Name="Window"
  9. Title="MainWindow"
  10. Width="640" Height="480">
  11.  
  12. <StackPanel Orientation="Vertical">
  13.     <StackPanel.Resources>
  14.         <ComboBoxItem x:Key="toSelectInitially" Content="{Binding Path=ActiveItem,   Mode=OneWay}"/>
  15.     </StackPanel.Resources>
  16.  
  17.     <ComboBox SelectedIndex="0"
  18.               Height="30">
  19.         <ComboBox.ItemsSource>
  20.             <x:Array Type="{x:Type ComboBoxItem}">
  21.                 <ComboBoxItem Content="{Binding Path=ActiveItem, Mode=OneWay}"/>
  22.                 <ComboBoxItem Content="AAA"/>
  23.                 <ComboBoxItem Content="BBB"/>
  24.             </x:Array>
  25.         </ComboBox.ItemsSource>
  26.     </ComboBox>
  27.  
  28.     <ComboBox SelectedItem="{StaticResource ResourceKey=toSelectInitially}"
  29.               Height="30" Loaded="ComboBox_Loaded">
  30.         <ComboBox.ItemsSource>
  31.             <x:Array Type="{x:Type ComboBoxItem}">
  32.                 <StaticResource ResourceKey="toSelectInitially"/>
  33.                 <ComboBoxItem Content="AAA"/>
  34.                 <ComboBoxItem Content="BBB"/>
  35.             </x:Array>
  36.         </ComboBox.ItemsSource>
  37.     </ComboBox>
  38.  
  39.     <ComboBox SelectedValue="{Binding Path=ActiveItem, Mode=OneWay}"
  40.               SelectedValuePath="Content"
  41.               Height="30">
  42.         <ComboBox.ItemsSource>
  43.             <x:Array Type="{x:Type ComboBoxItem}">
  44.                 <ComboBoxItem Content="{Binding Path=ActiveItem, Mode=OneWay}"/>
  45.                 <ComboBoxItem Content="AAA"/>
  46.                 <ComboBoxItem Content="BBB"/>
  47.             </x:Array>
  48.         </ComboBox.ItemsSource>
  49.     </ComboBox>
  50.  
  51. </StackPanel>
  52.  
  53.  
  54. </Window>
  55.        
  56. namespace WpfApplication1
  57. {
  58. /// <summary>
  59. /// Interaction logic for MainWindow.xaml
  60. /// </summary>
  61. public partial class MainWindow : Window
  62. {
  63.     public MainWindow()
  64.     {
  65.         this.InitializeComponent();
  66.         this.DataContext = new VMSimple();
  67.     }
  68. }
  69. }
  70.        
  71. using System;
  72. using System.ComponentModel;
  73.  
  74. namespace WpfApplication1
  75. {
  76. class VMSimple : INotifyPropertyChanged
  77. {
  78.     public VMSimple()
  79.     {
  80.         ActiveItem = string.Concat("Active Item : ", Guid.NewGuid().ToString());
  81.     }
  82.  
  83.     private string mActiveItem;
  84.     public string ActiveItem
  85.     {
  86.         get { return mActiveItem; }
  87.         private set
  88.         {
  89.             if (Equals(mActiveItem, value)) return;
  90.             mActiveItem = value;
  91.             OnPropertyChanged("ActiveItem");
  92.         }
  93.     }
  94.  
  95.     public event PropertyChangedEventHandler PropertyChanged;
  96.  
  97.     private void OnPropertyChanged(string propertyName)
  98.     {
  99.         if (PropertyChanged != null)
  100.             PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  101.     }
  102. }
  103. }
  104.        
  105. <ComboBox ItemsSource="{Binding Path=ComboItems}" SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"/>
  106.        
  107. public class ViewModel : INotifyPropertyChanged
  108. {
  109.     private List<string> m_ComboItems= new List<string>();
  110.     private string m_SelectedItem;
  111.  
  112.     public ViewModel()
  113.     {
  114.         m_ComboItems.Add("AA");
  115.         m_ComboItems.Add("BB");
  116.         m_ComboItems.Add("CC");
  117.         this.SelectedItem = m_ComboItems.First<string>();
  118.     }
  119.  
  120.     public List<string> ComboItems
  121.     {
  122.         get { return m_ComboItems;}            
  123.     }
  124.  
  125.     public string SelectedItem
  126.     {
  127.         get { return m_SelectedItem; }
  128.         set
  129.         {
  130.             m_SelectedItem = value;
  131.             this.OnPropertyChanged("SelectedItem");
  132.         }
  133.     }
  134.  
  135.     public event PropertyChangedEventHandler PropertyChanged;
  136.  
  137.     protected void OnPropertyChanged(string propertyName)
  138.     {
  139.         if (this.PropertyChanged != null)
  140.             this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  141.     }
  142. }
  143.        
  144. <ComboBox IsSynchronizedWithCurrentItem="True"/>