ThicknessPropertyConverter to bind several attributes of Thickness

Source: Internet
Author: User

The Margin type is Thickness, and the Top and Left attributes of Thickness are not dependency attributes and cannot be bound separately. There are many posts on the Internet asking how to bind a certain (several) attribute of Margin, such as (Sorry, I have not found any related questions or introductions in the Chinese circle) binding only part of the margin property of WPF controlBinding just one MarginHow to set a top margin only in XAML? [Csharp] <Slider Name = "slider1" Grid. row = "0" Maximum = "200" Value = "100"/> <Line Grid. row = "2" HorizontalAlignment = "Left" Y2 = "1" Stretch = "Fill" Stroke = "Black" StrokeThickness = "2" Margin. left = "{Binding Element = slider1 Value = Path}"/> </Line> As mentioned earlier, Margin. left does not work because Left is not a dependency attribute. Some methods are provided on the Internet. Most of them are dedicated to providing a custom converter (ValueConverter) for a specific attribute, such as MarginTopConverter and MarginLeftConverter. To solve this uncommon defect, I wrote a ThicknessPropertyConverter. The name contains Thickness, because the Margin type is Thickness; The name has Property, because it can be bound to any or several attributes of Thickness. Next I will paste ThicknessPropertyConverter's usage, effects, and code. Usage [html] <Window x: Class = "WpfApplication1.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: Data = "clr-namespace: Gqqnbig. windows. data "Title =" MainWindow "Height =" 350 "Width =" 525 "> <Window. resources> www.2cto.com <Data: ThicknessPropertyConverter x: Key = "thicknessSingleConverter"/> </Window. resources> <Grid. RowDefinitions> <RowDefinition Height = "auto"/> <RowDefinition Height = "auto"/> <RowDefinition/> </Grid. rowDefinitions> <Slider Name = "slider1" Grid. row = "0" Maximum = "200" Value = "100"/> <Slider Name = "slider2" Grid. row = "1" Maximum = "200" Value = "100"/> <Line Grid. row = "2" HorizontalAlignment = "Left" Y2 = "1" Stretch = "Fill" Stroke = "Black" StrokeThickness = "2"> <Line. margin> <MultiBinding Converter = "{statrn Esource thicknessSingleConverter} "ConverterParameter =" {}{ 0} {1} 0 0 "> <Binding ElementName =" slider1 "Path =" Value "/> <Binding ElementName =" slider2" path = "Value"/> </MultiBinding> </Line. margin> </Line> </Grid> </Window> there are two slider and a vertical black Line. The first slider controls the Margin. Left of the black line, and the second slider controls the Margin. Top of the black line. Effect

Recommended code file name: ThicknessPropertyConverter. cs. The code in this document is published according to the MIT license. [Csharp]/* This code is issued according to the MIT license. For the specific terms of this License, refer to Wikipedia http://zh.wikipedia.org/zh-cn/MIT%E8%A8%B1%E5%8F%AF%E8%AD%89 Copyright (c) 2013 love to make everything right (Gqqnbig) gqqnb2005@gmail.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights Use, copy, modify, merge, publish, distribute, sublicense, and/or duplicate copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be encoded in all copies or substantial portions of the Software. the software is provided "as is", without warranty of any kind, EXPRESS OR IMPLIED, including but not limited to the warranties of merchantability, fitness for a particle purpose and noninfringement. in no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. */namespace G Qqnbig. windows. data {using System; using System. globalization; using System. windows; using System. windows. data; [ValueConversion (typeof (double), typeof (Thickness)] partial // partial allows you to create another branch class of this class and change the access, for example, change to public, you do not need to modify this file. Class ThicknessPropertyConverter: IValueConverter, IMultiValueConverter {private static readonly ThicknessConverter thicknessConverter = new ThicknessConverter (); # region Implementation of IValueConverter // <summary> /// </summary> /// <param name = "value"> </param> /// <param name = "targetType"> </param> // <param name = "parameter"> formatted string with {0, other formats must comply with ThicknessConverter. </Param> /// <param name = "culture"> </param> /// <returns> </returns> public object Convert (object value, Type targetType, object parameter, CultureInfo culture) {try {return thicknessConverter. convertFromString (string. format (string) parameter), value);} catch (InvalidCastException e) {throw new ArgumentException ("parameter must be of the string type", e );} catch (FormatException e) {throw new ArgumentException ("Parameter must comply with the rules for formatting strings", e);} catch (NotSupportedException e) {throw new ArgumentException ("string. format (string) parameter), value) must generate a valid Thickness string expression ", e) ;}} public object ConvertBack (object value, Type targetType, object parameter, cultureInfo culture) {throw new NotImplementedException () ;}# endregion # region Implementation of IMultiValueConverter // <summary> //// </summary> // /<Param name = "values"> </param> // <param name = "targetType"> </param> // <param name = "parameter"> format String from {0} to {3, other formats must comply with ThicknessConverter. </Param> /// <param name = "culture"> </param> /// <returns> </returns> public object Convert (object [] values, type targetType, object parameter, CultureInfo culture) {if (values. length> 4) throw new ArgumentException ("ThicknessPropertyConverter can be converted from up to four data types. "," Values "); try {return thicknessConverter. convertFromString (string. format (string) parameter), values);} catch (InvalidCastException e) {throw new ArgumentException ("parameter must be of the string type", e );} catch (FormatException e) {throw new ArgumentException ("parameter must comply with the formatting string", e);} catch (NotSupportedException e) {throw new ArgumentException ("string. format (string) parameter), values) must generate a valid Thickness string expression ", e) ;}} public object [] ConvertBack (object value, Type [] targetTypes, object parameter, CultureInfo culture) {throw new NotImplementedException () ;}# endregion }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.