如何:使用 ThicknessConverter 对象

示例

本示例演示如何创建 ThicknessConverter 的实例,并用它来更改边框的粗细。

此示例定义了一个称为 changeThickness 的自定义方法,此方法首先将单独 Extensible Application Markup Language (XAML) 文件中定义的 ListBoxItem 的内容转换为 Thickness 的实例,然后再将该内容转换为 String。 此方法将 ListBoxItem 传递给 ThicknessConverter 对象,该对象将 ListBoxItemContent 转换为 Thickness 的实例。 此值然后作为 BorderBorderThickness 属性值进行回传。

此示例不运行。

Private Sub changeThickness(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)

    Dim li As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
    Dim myThicknessConverter As System.Windows.ThicknessConverter = New System.Windows.ThicknessConverter()
    Dim th1 As Thickness = CType(myThicknessConverter.ConvertFromString(li.Content.ToString()), Thickness)
    border1.BorderThickness = th1
    bThickness.Text = "Border.BorderThickness =" + li.Content.ToString()
End Sub
        private void changeThickness(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
            ThicknessConverter myThicknessConverter = new ThicknessConverter();
            Thickness th1 = (Thickness)myThicknessConverter.ConvertFromString(li.Content.ToString());
            border1.BorderThickness = th1;
            bThickness.Text = "Border.BorderThickness =" + li.Content.ToString();
        }

请参见

任务

如何:更改 Margin 属性

如何:将 ListBoxItem 转换到新的数据类型

参考

Thickness

ThicknessConverter

Border

概念

面板概述