WPF 绑定的时候一般都是单纯的将值复制。这样很不灵活。 例如。某控件值要是另一个控件值高固定值(a=1,b=a+1),或者是另一个控件值的几倍(a=1,b=a*4) 等。 再或者想要一个控件一直占它容器的1/4大小等。 或者某个文本框自动设置为绑定值的一半等。。。
提到的这些应用其实是比较多的。所以Kyler就在这里写了一个转换器。代码可能有点粗糙。主要是介绍一个思路和学习。大家可以按照这个思路去实现更多的应用上去。 废话不说,上代码
AutoSizeDemo.xaml
<Window x:Class="Kyler.AutoSizeDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Kyler" Title="AutoSizeDemo" Height="256" Width="501"> <Window.Resources> <local:NumCover x:Key="numCover"/> </Window.Resources> <Grid Name="contentGrid"> <Button Name="button1" HorizontalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Click="button1_Click"> <Button.Width> <Binding ElementName="contentGrid" Path="ActualWidth" Converter="{StaticResource numCover}" ConverterParameter="/,4"> </Binding> </Button.Width> <Button.Height> <Binding ElementName="contentGrid" Path="ActualHeight" Converter="{StaticResource numCover}" ConverterParameter="/,2"></Binding> </Button.Height> 宽为1/4,高1/2</Button> </Grid> </Window>
以上代码为默认显示的Window. 下面来逻辑代码 和 转换类
AutoSizeDemo.xmal.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Kyler { /// <summary> /// AutoSizeDemo.xaml 的交互逻辑 /// </summary> public partial class AutoSizeDemo : Window { public AutoSizeDemo() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { MessageBox.Show("容器大小:"+this.ActualWidth+","+this.ActualHeight+"\n按钮大小:"+button1.ActualWidth+","+button1.ActualHeight); } } }
转换类的代码
NumCover.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Data; namespace Kyler { class NumCover : IValueConverter { #region IValueConverter 成员 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return value; try { double v = (double)value; String parm = (String)parameter; String op = null; double num = double.NaN; if (parm != null) { String[] parms = parm.Split(",".ToCharArray(),2); op = parms[0]; if(parms.Length > 1) num = double.Parse(parms[1]); } if (double.IsNaN(num)) { return value; } switch (op) { case "+": return v + num; case "-": return v - num; case "*": return v * num; case "/": return v / num; case "P": return Math.Pow(v,num); case "L": return Math.Log(v, num); } } catch (Exception ex) { //throw ex; Console.WriteLine("转换数值出错:原始值:"+value+",转换参数:"+parameter+".消息:"+ex.Message); } return value; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return Convert(value,targetType,parameter,culture); } #endregion } }
到这里代码就全部结束了。很简单的功能。 作用在于用绑定但是又不想完全用原始值。希望有些修改。
原创技术文章。转载请注明出处。http://www.tk369.com/18.html
http://www.tk369.com/ - Kyler's Sky