当前位置:编程学习 > C#/ASP.NET >>

ThicknessPropertyConverter,以便绑定Thickness的某几个属性

Margin的类型是Thickness,而Thickness的Top、Left等属性不是依赖项属性,不能单独绑定。网上有许多帖子询问如何绑定到Margin的某(几)个属性,如
 
(抱歉,我没有在中文圈里搜到相关的问题或介绍)
Binding only part of the margin property of WPF control
Binding just one Margin
How 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>  
 
刚才说过,Margin.Left一句是行不通的,因为Left不是依赖项属性。
网上提供了一些办法,大多是专门针对某个属性,提供一个自定义转换器(ValueConverter),比如MarginTopConverter、MarginLeftConverter等;而不能通用。
 
为了解决这个不通用的缺陷,我写了一个ThicknessPropertyConverter。名称里有Thickness,因为Margin的类型是Thickness;名称里有Property,因为它可以绑定到Thickness的任意一个或几个属性。
 
下面我会贴上ThicknessPropertyConverter的用法、效果和代码。
 
用法
[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.zzzyk.com
        <Data:ThicknessPropertyConverter x:Key="thicknessSingleConverter"/>  
    </Window.Resources>  
    <Grid>  
        <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="{StaticResource thicknessSingleConverter}" ConverterParameter="{}{0} {1} 0 0">  
                    <Binding ElementName="slider1" Path="Value"/>  
                    <Binding ElementName="slider2" Path="Value"/>  
                </MultiBinding>  
            </Line.Margin>  
        </Line>  
    </Grid>  
</Window>  
 
这里有两个滑块(slider)和一条垂直的黑线。第一个滑块控制黑线的Margin.Left,第二个滑块控制黑线的Margin.Top。
效果
 


代码
推荐文件名:ThicknessPropertyConverter.cs。本文件的代码,依照MIT许可证发布。
 
 
[csharp]  
/* 
本代码依照 MIT许可证 发布,关于该许可证的具体条款,可参考维基百科 http://zh.wikipedia.org/zh-cn/MIT%E8%A8%B1%E5%8F%AF%E8%AD%89 
 
Copyright (c) 2013 爱让一切都对了(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 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 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 included 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 PARTICULAR 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 Gqqnbig.Windows.Data  
{  
    using System;  
    using System.Globalization;  
    using System.Windows;  
    using System.Windows.Data;  
  
  
  
    [ValueConversion(typeof(double), typeof(Thickness))]  
    partial //partial令到你可以创建本类的另一个分部类,更改访问性,如改为public,而不用修改本文件。  
        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">带有{0}的格式化字符串,其他格式必须遵守ThicknessConverter的规定。</param>  
        /// <param name="culture"></param>  
        /// <returns></returns>  
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,