tbiro.com

Forums 
   HackPerfCounter
   RADPass
   SHEdit
   Toys
 
   Contact me
   My PGP Key
 

ListBox Data Binding With Updating Templates



How to get this code going:
  • Create a Windows Presentation Foundation project named ListBoxSample3
  • Add a new class file named Computer.cs
  • Add a new class file named ComputerTemplateSelector.cs
  • Paste the following code into the corresponding files.

Replace the contents of Window1.xaml with this:

<Window x:Class="ListBoxSample3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="259" Width="291">

    <Window.Resources>
        <DataTemplate x:Key="compTemplate" x:Name="compTemplate">
            <StackPanel>
                <TextBlock FontSize="14" Text="{Binding Path=Name}" />
                <TextBlock FontSize="10" Text="{Binding Path=Location}" />
                <TextBlock FontSize="10" Text="{Binding Path=Make}" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="compBrokenTemplate" x:Name="compBrokenTemplate">
            <StackPanel>
                <TextBlock FontSize="14" Foreground="Red" Text="{Binding Path=Name}" />
                <TextBlock FontSize="10" Foreground="Red" Text="Broken" />
            </StackPanel>
        </DataTemplate>
    </Window.Resources>

    <Grid Name="mainGrid" Background="AntiqueWhite">
        <ListBox Name="computerList" Margin="0,0,0,52" />

        <Button Height="23" Name="button1" VerticalAlignment="Bottom" Click="button1_Click">Pick me!</Button>
        <Button Height="23" Width="120" Name="button2" VerticalAlignment="Bottom" HorizontalAlignment="Left" 
                Margin="10,0,0,25" Click="button2_Click">Break</Button>
        <Button Height="23" Width="120" Name="button3" VerticalAlignment="Bottom" HorizontalAlignment="Right" 
                Margin="0,0,10,25" Click="button3_Click">Fix</Button>

    </Grid>

</Window>

Replace the contents of Window1.xaml.cs with this:

using System.Windows;

namespace ListBoxSample3
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            ComputerTemplateSelector templateSelector = new ComputerTemplateSelector();

            templateSelector.CompTemplate = Resources["compTemplate"] as DataTemplate;
            templateSelector.CompBrokenTemplate = Resources["compBrokenTemplate"] as DataTemplate;

            computerList.ItemTemplateSelector = templateSelector;

            computerList.ItemsSource = Computer.SampleComputers();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Computer selectedComputer = computerList.SelectedItem as Computer;

            if (selectedComputer == null)
            {
                MessageBox.Show("Select something");
                return;
            }

            MessageBox.Show("You have selected " + selectedComputer.Name);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Computer selectedComputer = computerList.SelectedItem as Computer;

            if (selectedComputer == null)
            {
                MessageBox.Show("Select something");
                return;
            }

            selectedComputer.IsBoken = true;

            // update UI
            computerList.Items.Refresh();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Computer selectedComputer = computerList.SelectedItem as Computer;

            if (selectedComputer == null)
            {
                MessageBox.Show("Select something");
                return;
            }

            selectedComputer.IsBoken = false;

            // update UI
            computerList.Items.Refresh();

        }
    }
}


Replace the contents of Computer.cs with this:

using System;
using System.Collections.Generic;

namespace ListBoxSample3
{
    public class Computer
    {
        private static List<Computer> sampleComputers = null;

        public String Name { get; set; }
        public String Location { get; set; }
        public String Make { get; set; }
        public Boolean IsBoken { get; set; }


        public Computer(String name, String location, String make)
            : this(name, location, make, false)
        {
        }

        public Computer(String name, String location, String make, Boolean isBroken)
        {
            Name = name;
            Location = location;
            Make = make;
            IsBoken = isBroken;
        }

        public static IEnumerable<Computer> SampleComputers()
        {
            if (sampleComputers == null)
            {
                sampleComputers = new List<Computer>();

                sampleComputers.Add(new Computer("Kitchen PC", "On the kitchen counter", "DELL AX443"));
                sampleComputers.Add(new Computer("NAS", "Network attached storage", "NSLU2"));
                sampleComputers.Add(new Computer("Media1", "Media server", "DELL VISTA"));
                sampleComputers.Add(new Computer("XBOX360", "Playing more", "MS", true));
                sampleComputers.Add(new Computer("KidPC", "Kids play stuff", "HP1"));
            }

            return sampleComputers;
        }
    }
}

Replace the contents of ComputerTemplateSelector.cs with this:

using System.Windows;
using System.Windows.Controls;

namespace ListBoxSample3
{
    public class ComputerTemplateSelector : DataTemplateSelector
    {
        private DataTemplate compTemplate;
        private DataTemplate compBrokenTemplate;

        public DataTemplate CompTemplate
        {
            get { return compTemplate; }
            set { compTemplate = value; }
        }

        public DataTemplate CompBrokenTemplate
        {
            get { return compBrokenTemplate; }
            set { compBrokenTemplate = value; }
        }

        public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
        {
            Computer computer = item as Computer;

            if (computer != null && !computer.IsBoken)
            {
                return compTemplate;
            }
            else
            {
                return compBrokenTemplate;
            }
        }
    }
}

Compile and run.



Comment in the forum





Sitemap generated by Sitemap Manager