Bounded to ComboBox : ComboBox « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » ComboBox 
24.19.5.Bounded to ComboBox
<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:WpfApplication1"
  Title="DisplayAndValueMemberBinding"
  Height="300"
  Width="300">
  <Window.Resources>
    <local:People x:Key="Family">
      <local:Person Name="A" Age="11" />
      <local:Person Name="B" Age="12" />
      <local:Person Name="C" Age="38" />
    </local:People>

    <local:NamedAges x:Key="NamedAgeLookup">
      <local:NamedAge NameForAge="zero" AgeId="0" />
      <local:NamedAge NameForAge="one" AgeId="1" />
      <local:NamedAge NameForAge="two" AgeId="2" />
    </local:NamedAges>
  </Window.Resources>
  <StackPanel DataContext="{StaticResource Family}">
    <ListBox Name="lb" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Name" SelectedValuePath="Age" />
    <TextBox Grid.Row="1" Text="{Binding Path=Name}" />
    <ComboBox Grid.Row="2" ItemsSource="{Binding Source={StaticResource NamedAgeLookup}}" DisplayMemberPath="NameForAge" SelectedValuePath="AgeId" SelectedValue="{Binding Path=Age}" />
      <Button Name="birthdayButton">Birthday</Button>
      <Button Name="backButton">Back</Button>
      <Button Name="forwardButton">Forward</Button>
  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;


namespace WpfApplication1 {

  public class Person : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    protected void Notify(string propName) {
      ifthis.PropertyChanged != null ) {
        PropertyChanged(this, new PropertyChangedEventArgs(propName));
      }
    }

    string name;
    public string Name {
      get return this.name; }
      set {
        this.name = value;
        Notify("Name");
      }
    }

    int age;
    public int Age {
      get return this.age; }
      set {
        this.age = value;
        Notify("Age");
      }
    }
  }

  class People : ObservableCollection<Person> { }

  public class NamedAge : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    protected void Notify(string propNameForAge) {
      ifthis.PropertyChanged != null ) {
        PropertyChanged(this, new PropertyChangedEventArgs(propNameForAge));
      }
    }

    string nameForAge;
    public string NameForAge {
      get return this.nameForAge; }
      set {
        this.nameForAge = value;
        Notify("NameForAge");
      }
    }

    int ageId;
    public int AgeId {
      get return this.ageId; }
      set {
        this.ageId = value;
        Notify("AgeId");
      }
    }
  }

  class NamedAges : ObservableCollection<NamedAge> { }

  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();
      lb.MouseDoubleClick += lb_MouseDoubleClick;
      this.birthdayButton.Click += birthdayButton_Click;
      this.backButton.Click += backButton_Click;
      this.forwardButton.Click += forwardButton_Click;
    }

    void lb_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
      int index = lb.SelectedIndex;
      ifindex < ) { return}

      Person item = (Person)lb.SelectedItem;
      int value = (int)lb.SelectedValue;

      Console.WriteLine(index);
      Console.WriteLine(item.Name);
      Console.WriteLine(item.Age);
      Console.WriteLine(value);
    }

    ICollectionView GetFamilyView() {
      People people = (People)this.FindResource("Family");
      return CollectionViewSource.GetDefaultView(people);
    }

    void birthdayButton_Click(object sender, RoutedEventArgs e) {
      ICollectionView view = GetFamilyView();
      Person person = (Person)view.CurrentItem;

      ++person.Age;
      Console.WriteLine(person.Name);
      Console.WriteLine(person.Age);
    }

    void backButton_Click(object sender, RoutedEventArgs e) {
      ICollectionView view = GetFamilyView();
      view.MoveCurrentToPrevious();
      ifview.IsCurrentBeforeFirst ) {
        view.MoveCurrentToFirst();
      }
    }

    void forwardButton_Click(object sender, RoutedEventArgs e) {
      ICollectionView view = GetFamilyView();
      view.MoveCurrentToNext();
      ifview.IsCurrentAfterLast ) {
        view.MoveCurrentToLast();
      }
    }

  }
}
WPF Bounded To Combo Box
24.19.ComboBox
24.19.1.ComboBoxItem ContentComboBoxItem Content
24.19.2.Content in ComboBox ItemsContent in ComboBox Items
24.19.3.Set Different Font for Item for ComboBoxSet Different Font for Item for ComboBox
24.19.4.Adding Image to ComboBox ItemAdding Image to ComboBox Item
24.19.5.Bounded to ComboBoxBounded to ComboBox
24.19.6.Gets the currently selected ComboBoxItem when the user clicks the Button.Gets the currently selected ComboBoxItem when the user clicks the Button.
24.19.7.if the user has entered text into the ComboBoxif the user has entered text into the ComboBox
24.19.8.Handles ComboBox SelectionChanged events.Handles ComboBox SelectionChanged events.
24.19.9.Handles ComboBoxItem Selected events.Handles ComboBoxItem Selected events.
24.19.10.View and Select Items Using a Combo BoxView and Select Items Using a Combo Box
24.19.11.Call the OnPropertyChanged method when its value changedCall the OnPropertyChanged method when its value changed
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.