OpenFileDialog Test

image_pdfimage_print


   
  

<Window x:Class="Windows.OpenFileTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="OpenFileTest" Height="300" Width="300" 
    >
  <DockPanel Margin="5">
    <Button DockPanel.Dock="Top" Click="cmdOpen_Click">Open</Button>
    <ListBox Name="lstFiles"></ListBox>
  </DockPanel>
</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 Microsoft.Win32;

namespace Windows
{
    public partial class OpenFileTest : System.Windows.Window
    {
        public OpenFileTest()
        {
            InitializeComponent();
        }

        private void cmdOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog myDialog = new OpenFileDialog();

            myDialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";
            myDialog.CheckFileExists = true;
            myDialog.Multiselect = true;

            if (myDialog.ShowDialog() == true)
            {
                lstFiles.Items.Clear();
                foreach (string file in myDialog.FileNames)
                {
                    lstFiles.Items.Add(file);
                }                
            }
        }
    }
}

   
    
     


ContextMenu Demo

image_pdfimage_print


   
   

<Window x:Class="SimpleStyles.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SimpleStyles"
  Background="#F8F8F8">
  <ScrollViewer>
    <WrapPanel>
      <HeaderedItemsControl Header="ContextMenu">
        <StackPanel>
          <Border Margin="8" Background="#EEE" Width="150" Height="50"  CornerRadius="2">
            <Border.ContextMenu>
              <ContextMenu>
                <MenuItem Header="Sub One" InputGestureText="Ctrl+L" />
                <MenuItem Header="Sub Two (With an Icon)" InputGestureText="Ctrl+A">
                  <MenuItem.Icon>
                    <Ellipse Width="16" Height="16" Fill="LightBlue" />
                  </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Sub Three" />
                <Separator />
                <MenuItem Header="Sub Four">
                  <MenuItem Header="Sub One" />
                  <MenuItem Header="Sub Two" />
                  <MenuItem Header="Sub Three" />
                </MenuItem>
                <MenuItem Header="Sub Five" />
              </ContextMenu>
            </Border.ContextMenu>
            <TextBlock Foreground="#AAA" VerticalAlignment="Center" HorizontalAlignment="Center">(Right-Click Me)</TextBlock>
          </Border>
        </StackPanel>
      </HeaderedItemsControl>
   
    </WrapPanel>
  </ScrollViewer>
</Window>

   
    
    
     


Adding Image to ComboBox Item

image_pdfimage_print
   
       

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Use ComboBox" Height="300" Width="300">
    <Grid>
        <ComboBox Height="39" HorizontalAlignment="Left" Margin="10,10,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="120" SelectedIndex="0" VerticalContentAlignment="Center">
            <ComboBoxItem>Apple</ComboBoxItem>
            <ComboBoxItem FontFamily="Comic Sans MS" FontSize="18" Foreground="Blue">Banana</ComboBoxItem>
            <ComboBoxItem Name="cbiSmile">
                <StackPanel Orientation="Horizontal">
                    <Image Source="c:image.bmp" Width="15" Height="15" />
                    <Label Content="Happy!" />
                </StackPanel>
            </ComboBoxItem>
            <ComboBoxItem>Cherry</ComboBoxItem>
        </ComboBox>
    </Grid>
</Window>

   
    
    
    
    
    
    
     


Bounded to ComboBox

image_pdfimage_print





















//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) {
if( this.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 { }

public class NamedAge : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string propNameForAge) {
if( this.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 { }

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;
if( index < 0 ) { 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(); if( view.IsCurrentBeforeFirst ) { view.MoveCurrentToFirst(); } } void forwardButton_Click(object sender, RoutedEventArgs e) { ICollectionView view = GetFamilyView(); view.MoveCurrentToNext(); if( view.IsCurrentAfterLast ) { view.MoveCurrentToLast(); } } } } [/csharp]

Gets the currently selected ComboBoxItem when the user clicks the Button.

image_pdfimage_print


   
  
<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 (!IsInitialized) return;

            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 (!IsInitialized) return;

            ComboBoxItem item = e.OriginalSource as ComboBoxItem;

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

   
    
     


if the user has entered text into the ComboBox instead.

image_pdfimage_print


   
  
<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 (!IsInitialized) return;

            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 (!IsInitialized) return;

            ComboBoxItem item = e.OriginalSource as ComboBoxItem;

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

   
    
     


Handles ComboBox SelectionChanged events.

image_pdfimage_print


   
  
<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 (!IsInitialized) return;

            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 (!IsInitialized) return;

            ComboBoxItem item = e.OriginalSource as ComboBoxItem;

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