View and Select Items Using a Combo Box : 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.10.View and Select Items Using a Combo Box
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="100" Width="300">
    <StackPanel>
        <ComboBox Name="comboBox" IsEditable="True" Margin="5" SelectionChanged="ComboBox_SelectionChanged">
            <ComboBoxItem Content="ComboBox Item 1" Selected="ComboBoxItem_Selected" />
            <ComboBoxItem Content="ComboBox Item 2" Selected="ComboBoxItem_Selected" />
            <ComboBoxItem Content="ComboBox Item 3" Selected="ComboBoxItem_Selected" IsSelected="True"/>
            <ComboBoxItem Content="ComboBox Item 4" Selected="ComboBoxItem_Selected" />
            <ComboBoxItem Content="ComboBox Item 5" Selected="ComboBoxItem_Selected" />
        </ComboBox>
        <Button Content="Get Selected" Margin="5" Width="100" Click="Button_Click" />
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ComboBoxItem item = comboBox.SelectedItem as ComboBoxItem;

            if (item != null)
            {
                MessageBox.Show("Current item: " + item.Content, Title);
            }
            else if (!String.IsNullOrEmpty(comboBox.Text))
            {
                MessageBox.Show("Text entered: " + comboBox.Text, Title);
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (!IsInitializedreturn;

            ComboBoxItem item = comboBox.SelectedItem as ComboBoxItem;

            if (item != null)
            {
                MessageBox.Show("Selected item: " + item.Content, Title);
            }
        }

        private void ComboBoxItem_Selected(object sender,RoutedEventArgs e)
        {
            if (!IsInitializedreturn;

            ComboBoxItem item = e.OriginalSource as ComboBoxItem;

            if (item != null)
            {
                MessageBox.Show(item.Content + " was selected.", Title);
            }
        }
    }
}
WPF View And Select Items Using A 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.