ListBox of Color Shapes : ListBox Data « Windows Presentation Foundation « C# / CSharp Tutorial






using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

    class ListColorShapes : Window
    {
        public static void Main()
        {
            Application app = new Application();
            app.Run(new ListColorShapes());
        }
        public ListColorShapes()
        {
            ListBox lstbox = new ListBox();
            lstbox.Width = 150;
            lstbox.Height = 150;
            lstbox.SelectionChanged += ListBoxOnSelectionChanged;
            Content = lstbox;

            PropertyInfo[] props = typeof(Brushes).GetProperties();
            foreach (PropertyInfo prop in props)
            {
                Ellipse ellip = new Ellipse();
                ellip.Width = 10;
                ellip.Height = 25;
                ellip.Margin = new Thickness(10, 5, 0, 5);
                ellip.Fill = prop.GetValue(null, null) as Brush;
                lstbox.Items.Add(ellip);
            }
        }
        void ListBoxOnSelectionChanged(object sender,SelectionChangedEventArgs args)
        {
            ListBox lstbox = sender as ListBox;

            if (lstbox.SelectedIndex != -1)
                Background = (lstbox.SelectedItem as Shape).Fill;
        }
    }








24.32.ListBox Data
24.32.1.Convert the contents of a ListBoxItem to an instance of GridLength by using GridLengthConverterConvert the contents of a ListBoxItem to an instance of GridLength by using GridLengthConverter
24.32.2.Use the FontSizeConverter class to convert the content of a ListBoxItem to a value that represents the size of a font.Use the FontSizeConverter class to convert the content of a ListBoxItem to a value that represents the size of a font.
24.32.3.Display Bounded objects onto ListBoxDisplay Bounded objects onto ListBox
24.32.4.Use DataTemplate in ListBoxUse DataTemplate in ListBox
24.32.5.ListBox binds to the people collection, and sets the DataTemplate to use for displaying each itemListBox binds to the people collection, and sets the DataTemplate to use for displaying each item
24.32.6.Without specifying a DataTemplate, the ListBox displays a list of names.Without specifying a DataTemplate, the ListBox displays a list of names.
24.32.7.Convert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverterConvert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverter
24.32.8.Convert contents of a ListBoxItem to an instance of Thickness by using the BrushConverterConvert contents of a ListBoxItem to an instance of Thickness by using the BrushConverter
24.32.9.DataTrigger, ListBox and user objectDataTrigger, ListBox and user object
24.32.10.Select Product Page FunctionSelect Product Page Function
24.32.11.Create ListBox and Add string as the ListBoxItem
24.32.12.ListBox of Color Values
24.32.13.ListBox of Color Shapes