Convert contents of a ListBoxItem to an instance of Thickness by using the ThicknessConverter
<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
Related examples in the same category