Convert contents of a ListBoxItem to an instance of Thickness by using the BrushConverter : ListBox Data « Windows Presentation Foundation « C# / CSharp Tutorial






<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ThicknessConverter_Csharp.Window1"
    Title="ThicknessConverter Sample">
  <DockPanel Width="400" HorizontalAlignment="Left" VerticalAlignment="Top">
    <TextBlock DockPanel.Dock="Top" FontFamily="Verdana" FontSize="18" FontWeight="Bold" Margin="5">
      ThicknessConverter Sample</TextBlock>
    <Border Name="border1" Height="300" Width="300" Border.BorderThickness="2" Border.BorderBrush="Black" DockPanel.Dock="Top">
      <Grid Height="30" VerticalAlignment="Top">
        <Grid.RowDefinitions>
          <RowDefinition/>
          <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Name="bThickness"/>
        <TextBlock Grid.Row="1" Name="bColor"/>
      </Grid>
    </Border>
    <Grid DockPanel.Dock="Top">
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <TextBlock Grid.Row="1" Grid.Column="2" TextWrapping="Wrap">Set the BorderBrush Property:</TextBlock>
      <ListBox VerticalAlignment="Top" Grid.Column="3" Grid.Row="1" Width="60" Height="50" Margin="5" SelectionChanged="changeColor">
        <ListBoxItem>Red</ListBoxItem>
        <ListBoxItem>Green</ListBoxItem>
        <ListBoxItem>Blue</ListBoxItem>
        <ListBoxItem>Yellow</ListBoxItem>
        <ListBoxItem>Orange</ListBoxItem>
        <ListBoxItem>Purple</ListBoxItem>
        <ListBoxItem>Silver</ListBoxItem>
        <ListBoxItem>Pink</ListBoxItem>
        <ListBoxItem>Maroon</ListBoxItem>
        <ListBoxItem>Brown</ListBoxItem>
        <ListBoxItem>Black</ListBoxItem>
      </ListBox>
    </Grid>
  </DockPanel>        
</Window>

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

namespace ThicknessConverter_Csharp
{
  public partial class Window1 : Window
  {

    public void changeColor(object sender, SelectionChangedEventArgs args)
    {
      ListBoxItem li2 = ((sender as ListBox).SelectedItem as ListBoxItem);
            BrushConverter myBrushConverter = new BrushConverter();
            border1.BorderBrush = (Brush)myBrushConverter.ConvertFromString((string)li2.Content);
            bColor.Text = "Border.Borderbrush =" + li2.Content.ToString();
        }
  }
}
WPF Convert Contents Of A List Box Item To An Instance Of Thickness By Using The Brush Converter








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