Convert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverter : ListBox « Windows Presentation Foundation « VB.Net 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="0" Margin="10,0,0,0" TextWrapping="Wrap">Change the BorderThickness Property:</TextBlock>
      <ListBox VerticalAlignment="Top" Grid.Column="1" Grid.Row="1" Width="50" Height="50" Margin="5" SelectionChanged="changeThickness">
        <ListBoxItem>0</ListBoxItem>
        <ListBoxItem>5</ListBoxItem>
        <ListBoxItem>10</ListBoxItem>
        <ListBoxItem>15</ListBoxItem>
        <ListBoxItem>20</ListBoxItem>
        <ListBoxItem>25</ListBoxItem>
        <ListBoxItem>30</ListBoxItem>
        <ListBoxItem>35</ListBoxItem>
        <ListBoxItem>40</ListBoxItem>
        <ListBoxItem>45</ListBoxItem>
        <ListBoxItem>50</ListBoxItem>
      </ListBox>
    </Grid>
  </DockPanel>        
</Window>

//File:Window.xaml.vb
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Navigation
Imports System.Windows.Media

Namespace ThicknessConverter_Csharp
  Public Partial Class Window1
    Inherits Window
    Public Sub changeThickness(sender As Object, args As SelectionChangedEventArgs)
      Dim li As ListBoxItem = TryCast(TryCast(sender, ListBox).SelectedItem, ListBoxItem)
      Dim myThicknessConverter As New ThicknessConverter()
      Dim th1 As Thickness = CType(myThicknessConverter.ConvertFromString(li.Content.ToString()), Thickness)
      border1.BorderThickness = th1
      bThickness.Text = "Border.BorderThickness =" & li.Content.ToString()
    End Sub
  End Class
End Namespace
WPF Convert Contents Of A List Box Item To An Instance Of Thickness By Using The Thickness Converter








16.27.ListBox
16.27.1.Bind String Array Resource to ListBoxBind String Array Resource to ListBox
16.27.2.Use Data Templates to Display Bound DataUse Data Templates to Display Bound Data
16.27.3.CheckBox ListCheckBox List
16.27.4.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
16.27.5.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.
16.27.6.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
16.27.7.DataTrigger, ListBox and user objectDataTrigger, ListBox and user object
16.27.8.Add selected file to ListBoxAdd selected file to ListBox
16.27.9.This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.This list box allows items to be selected in groups by using the SHIFT key and mouse or the CTRL key and space key.
16.27.10.Iterate through the selected items and remove each oneIterate through the selected items and remove each one
16.27.11.Ensure there is at least one item selectedEnsure there is at least one item selected