Use ObservableCollection as Resource


   
  
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:ListBoxEvent"  x:Class="ListBoxEvent.Pane1">
  <Canvas.Resources>
    <src:myColors x:Key="Colors"/>
  </Canvas.Resources>

  <StackPanel>
      <TextBox Name="tb" Width="140" Height="30"></TextBox>
      <ListBox Name="lb" Width="100" Height="55" SelectionChanged="PrintText" SelectionMode="Single">
        <ListBoxItem>Item 1</ListBoxItem>
        <ListBoxItem>Item 2</ListBoxItem>
        <ListBoxItem>Item 3</ListBoxItem>
        <ListBoxItem>Item 4</ListBoxItem>
        <ListBoxItem>Item 5</ListBoxItem>
        <ListBoxItem>Item 6</ListBoxItem>
        <ListBoxItem>Item 7</ListBoxItem>
        <ListBoxItem>Item 8</ListBoxItem>
        <ListBoxItem>Item 9</ListBoxItem>
        <ListBoxItem>Item 10</ListBoxItem>
      </ListBox>
  </StackPanel>

</Canvas>
//File:Window.xaml.cs

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media;
using System.Collections.ObjectModel;

namespace ListBoxEvent
{
    public class myColors : ObservableCollection<string>
    {
        public myColors()
        {
            Add("A");
            Add("B");
            Add("C");
            Add("D");
            Add("E");
            Add("F");
        }
    }
    public partial class Pane1 : Canvas
    {

        public Pane1() : base(){
            InitializeComponent();
        }
        void PrintText(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
            tb.Text = "   You selected " + lbi.Content.ToString() + ".";
        }
    }
}

   
    
     


Create ObjectDataProvider and bind object to it in code


   
  


<Window x:Class="WpfApplication1.Monitor"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=System"
    xmlns:debug="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    Title="Monitor" Height="400" Width="400">
    <Grid>
        <ListView Name="listView1">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Path=Id}" MinWidth="80" />
                        <TextBlock Text="{Binding Path=ProcessName}" MinWidth="180" />
                        <TextBlock>
                                <TextBlock.Text>
                                    <Binding Path="WorkingSet" />
                                </TextBlock.Text>
                        </TextBlock>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>



//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
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.Diagnostics;

namespace WpfApplication1
{
    public partial class Monitor : Window
    {
        public Monitor()
        {
            InitializeComponent();
            BindProcessesToListView();
        }

        private void BindProcessesToListView()
        {
            ObjectDataProvider provider = new ObjectDataProvider();
            provider.ObjectType = typeof(Process);
            provider.MethodName = "GetProcesses";
            Binding binding = new Binding();
            binding.Source = provider;
            binding.Mode = BindingMode.OneWay;

            listView1.SetBinding(ListView.ItemsSourceProperty, binding);
        }
    }
}

   
    
     


Set up ObjectDataProvider in code


   
  
<Window x:Class="WpfApplication1.Monitor"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:diag="clr-namespace:System.Diagnostics;assembly=System"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        Title="Monitor" Height="400" Width="389">
    <Grid>
        <Grid.Resources>
            <ObjectDataProvider x:Key="processes" MethodName="GetProcesses" ObjectType="{x:Type diag:Process}"/>
            <ObjectDataProvider x:Key="dateinfo" ObjectType="{x:Type system:DateTime}"/>
        </Grid.Resources>
        <ListView Name="listView1" ItemsSource="{Binding Source={StaticResource processes}}">
        </ListView>
    </Grid>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
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.Diagnostics;
using System.Windows.Markup;
using System.Xml;
using System.IO;
using System.Timers;

namespace WpfApplication1
{
    public partial class Monitor : Window
    {
        public Monitor()
        {
            InitializeComponent();
            ObjectDataProvider provider = new ObjectDataProvider();
            provider.ObjectType = typeof(Process);
            provider.MethodName = "GetProcesses";

            Binding binding = new Binding();
            PresentationTraceSources.SetTraceLevel(binding, PresentationTraceLevel.High);
            binding.Source = provider;
            binding.Mode = BindingMode.OneWay;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
            listView1.ItemTemplate = CreateItemTemplateFromXaml();
            listView1.SetBinding(ListView.ItemsSourceProperty, binding);
        }

        private DataTemplate CreateItemTemplateFromXaml()
        {
            string templateXml = "<DataTemplate xmlns=&#039;http://schemas.microsoft.com/winfx/2006/xaml/presentation&#039; xmlns:x=&#039;http://schemas.microsoft.com/winfx/2006/xaml&#039;><TextBlock Text=&#039;{Binding Path=ProcessName}&#039;/></DataTemplate>";
            byte[] templateBytes = Encoding.UTF8.GetBytes(templateXml);

            using (MemoryStream templateStream = new MemoryStream(templateBytes))
            {
                DataTemplate template = (DataTemplate)XamlReader.Load(templateStream);
                return template;
            }
        }

        private DataTemplate CreateItemTemplate()
        {
            DataTemplate template = new DataTemplate();

            FrameworkElementFactory panelFactory = new FrameworkElementFactory(typeof(WrapPanel));

            FrameworkElementFactory idTextFactory = new FrameworkElementFactory(typeof(TextBlock));
            idTextFactory.SetBinding(TextBlock.TextProperty, new Binding("Id"));
            idTextFactory.SetValue(TextBlock.MinWidthProperty, 80d);

            FrameworkElementFactory workingSetTextFactory = new FrameworkElementFactory(typeof(TextBlock));
            workingSetTextFactory.SetBinding(TextBlock.TextProperty, new Binding("WorkingSet"));

            panelFactory.AppendChild(idTextFactory);
            panelFactory.AppendChild(workingSetTextFactory);
            template.VisualTree = panelFactory;

            return template;
        }
    }
}

   
    
     


The ObjectDataProvider exposes the enum as a binding source


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1"
    Title="WPF" Height="100" Width="180">

    <Window.Resources>
        <ObjectDataProvider x:Key="daysData" MethodName="GetValues" ObjectType="{x:Type System:Enum}" >
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="WpfApplication1:DaysOfTheWeek"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

    </Window.Resources>
    
    <StackPanel>
        <TextBlock Margin="5" Text="Select the day of the week:"/>
        <ComboBox Margin="5" ItemsSource="{Binding Source={StaticResource daysData}}" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Windows;

namespace WpfApplication1
{
    public enum DaysOfTheWeek
    {
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday
    }

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
}

   
    
     


Bind to Object to ObjectDataProvider


   
  




<Window x:Class="WpfApplication1.Monitor"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=System"
    xmlns:debug="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    Title="Monitor" Height="400" Width="400">
    <Grid>
        <Grid.Resources>
            <ObjectDataProvider x:Key="processes" MethodName="GetProcesses" ObjectType="{x:Type diag:Process}"/>
        </Grid.Resources>
        <ListView Name="listView1" ItemsSource="{Binding Source={StaticResource processes}, debug:PresentationTraceSources.TraceLevel=High}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Path=Id}" MinWidth="80" />
                        <TextBlock Text="{Binding Path=ProcessName}" MinWidth="180" />
                        <TextBlock>
                            <TextBlock.Text>
                                <Binding Path="WorkingSet" />
                            </TextBlock.Text>
                        </TextBlock>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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;

namespace WpfApplication1
{
    public partial class Monitor : Window
    {
        public Monitor()
        {
            InitializeComponent();
        }
    }
    public class AddPaddingValueConverter : IValueConverter
    {
        public AddPaddingValueConverter() { }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double d = System.Convert.ToDouble(value);
            return d + 20;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double d = System.Convert.ToDouble(value);
            return d - 20;
        }
    }    
}

   
    
     


Bind to ObjectDataProvider


   
  

<Window x:Class="WpfApplication1.Monitor"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:diag="clr-namespace:System.Diagnostics;assembly=System"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        Title="Monitor" Height="400" Width="389">
    <Grid>
        <Grid.Resources>
            <ObjectDataProvider x:Key="processes" MethodName="GetProcesses" ObjectType="{x:Type diag:Process}"/>
            <ObjectDataProvider x:Key="dateinfo" ObjectType="{x:Type system:DateTime}"/>
        </Grid.Resources>
        <ListView Name="listView1" ItemsSource="{Binding Source={StaticResource processes}}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Path=Id}" MinWidth="80"/>
                        <TextBlock Text="{Binding Path=StartTime, StringFormat=d}" MinWidth="80"/>
                        <TextBlock Text="{Binding Path=ProcessName,StringFormat=Process {0}}" MinWidth="160"/>
                        <TextBlock Text="{Binding Path=WorkingSet, StringFormat={0:N0}}"/>
                        <TextBlock Text="{Binding Path=WorkingSet, StringFormat={0:N0} bytes}"/>
                        <TextBlock MinWidth="100" TextAlignment="Right">
              <TextBlock.Text>
                <Binding Path="WorkingSet" StringFormat="{}{0:N0} bytes"/>
              </TextBlock.Text>
            </TextBlock>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

   
    
     


Navigation Basics


   
  

<NavigationWindow
    x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="NavigationBasics"
    Height="300"
    Width="300">
</NavigationWindow>
//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.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Documents;
using System.Diagnostics;


namespace WpfApplication1 {
  public partial class Window1 : NavigationWindow {
    Hyperlink link1 = new Hyperlink();
    Hyperlink link2 = new Hyperlink();
    TextBlock text1 = new TextBlock();
    TextBlock text2 = new TextBlock();

    public Window1() {
      InitializeComponent();
      text1.Inlines.Add(link1);
      text1.VerticalAlignment = VerticalAlignment.Bottom;
      text1.Loaded += text1_Loaded;

      link1.Inlines.Add("Click to see page 2");
      link1.Click += link1_Click;

      text2.Inlines.Add(link2);
      text2.VerticalAlignment = VerticalAlignment.Bottom;
      text2.Loaded += text2_Loaded;

      link2.Inlines.Add("Click to go back to page 1");
      link2.Click += link2_Click;

      this.Navigate(text1);


      Button button1 = new Button();
      Button button2 = new Button();

      button1.Content = "Click to see Button 2";
      button1.Loaded += delegate(object sender, RoutedEventArgs e2) {
          ((NavigationWindow)button1.Parent).Title = "Welcome to button1";
      };
      button1.Click += delegate(object sender, RoutedEventArgs e2) {
          this.Navigate(button2);
      };
      button2.Content = "Click to go back to Button 1";
      button2.Loaded += delegate(object sender, RoutedEventArgs e2) {
          ((NavigationWindow)button2.Parent).Title = "Welcome to button2";
      };
      button2.Click += delegate(object sender, RoutedEventArgs e2) {
         ((NavigationWindow)button2.Parent).GoBack();
      };

      this.Navigate(button1);
    }

    void text1_Loaded(object sender, RoutedEventArgs e) {
      Title = "Welcome to Page 1";
    }

    void text2_Loaded(object sender, RoutedEventArgs e) {
      Title = "Welcome to Page 2";
    }

    void link1_Click(object sender, RoutedEventArgs e) {
      Navigate(text2);
    }

    void link2_Click(object sender, RoutedEventArgs e) {
      NavigationService navService = NavigationService.GetNavigationService((DependencyObject)sender);
      navService.GoBack();
    }
  }
}