[WPF学习]数据绑定

前阵子朋友发来一个Demo,说还是用的WINFORM的思想在写WPF程序,让我给看看有何改进之处,程序原帖如下:

 

点击打开链接


主要代码还是在四个Slider上绑定同一个事件处理函数:

 

  1. private void sliderValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e) 
  2.        { 
  3.            byte a = (byte)(sliderA.Value); 
  4.            byte r = (byte)(sliderR.Value); 
  5.            byte g = (byte)(sliderG.Value); 
  6.            byte b = (byte)(sliderB.Value); 
  7.  
  8.            Color clr = Color.FromArgb(a, r, g, b); 
  9.  
  10.            demoArea.Fill = new SolidColorBrush(clr); 
  11.            txtColorValue.Text = clr.ToString(); 
  12.        } 

 


而似乎这直接可以用WPF的数据绑定来完成的。

算是帮朋友解决问题,也算是自己学习一下WPF,写了个小Demo。


首先第一想法是绑定方法。

使用ObjectDataProvider。

但是由于ObjectInstance属性需要一个实例,对此很是纳闷,对于静态方法这该如何?看来自己的C#基础还是没有学好啊。

也罢,于是自己写了个类,只有一个方法,来调用Color的FromArgb方法。

 

  1. class MyClass 
  2.     { 
  3.         public Brush FromArgb(double a, double r, double g, double b) 
  4.         { 
  5.             return new SolidColorBrush(Color.FromArgb(Convert.ToByte(a), Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b))); 
  6.         } 
  7.     } 

然后,绑定:

 

 

  1. public partial class MainWindow : Window 
  2.     { 
  3.         public MainWindow() 
  4.         { 
  5.             InitializeComponent(); 
  6.         } 
  7.         ObjectDataProvider odp = new ObjectDataProvider(); 
  8.  
  9.         private void Window_Loaded(object sender, RoutedEventArgs e) 
  10.         { 
  11.             odp.ObjectInstance = new MyClass(); 
  12.             odp.MethodName = "FromArgb"
  13.             odp.MethodParameters.Add(0); 
  14.             odp.MethodParameters.Add(0); 
  15.             odp.MethodParameters.Add(0); 
  16.             odp.MethodParameters.Add(0); 
  17.  
  18.             Binding bind0 = new Binding("MethodParameters[0]")  
  19.             { 
  20.                 Source = odp, 
  21.                 BindsDirectlyToSource = true
  22.                 UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
  23.             }; 
  24.             Binding bind1 = new Binding("MethodParameters[1]"
  25.             { 
  26.                 Source = odp, 
  27.                 BindsDirectlyToSource = true
  28.                 UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
  29.             }; 
  30.             Binding bind2 = new Binding("MethodParameters[2]"
  31.             { 
  32.                 Source = odp, 
  33.                 BindsDirectlyToSource = true
  34.                 UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
  35.             }; 
  36.             Binding bind3 = new Binding("MethodParameters[3]"
  37.             { 
  38.                 Source = odp, 
  39.                 BindsDirectlyToSource = true
  40.                 UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
  41.             }; 
  42.             Binding bindResult = new Binding(".") { Source = odp }; 
  43.  
  44.             this.sliderA.SetBinding(Slider.ValueProperty, bind0); 
  45.             this.sliderR.SetBinding(Slider.ValueProperty, bind1); 
  46.             this.sliderG.SetBinding(Slider.ValueProperty, bind2); 
  47.             this.sliderB.SetBinding(Slider.ValueProperty, bind3); 
  48.  
  49.             this.rectColor.SetBinding(Grid.BackgroundProperty, bindResult); 
  50.         } 
  51.     } 

这解决方法有点别扭,似乎有意为了绑定而绑定。

 

所以又做了思考,决定使用MultiBinding(多重绑定)

于是写下如下代码:

 

  1. public class DoublesToBrushConverter : IMultiValueConverter 
  2.     { 
  3.         #region IMultiValueConverter 成员 
  4.  
  5.         public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
  6.         { 
  7.             return new SolidColorBrush(Color.FromArgb( 
  8.                 System.Convert.ToByte(values[0]),  
  9.                 System.Convert.ToByte(values[1]), 
  10.                 System.Convert.ToByte(values[2]), 
  11.                 System.Convert.ToByte(values[3]))); 
  12.         } 
  13.  
  14.         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
  15.         { 
  16.             throw new NotImplementedException(); 
  17.         } 
  18.         #endregion 
  19.     } 
  20.  
  21.     /// <summary> 
  22.     /// MainWindow.xaml 的交互逻辑 
  23.     /// </summary> 
  24.     public partial class Win1 : Window 
  25.     { 
  26.         public Win1() 
  27.         { 
  28.             InitializeComponent(); 
  29.         } 
  30.  
  31.         private void Window_Loaded(object sender, RoutedEventArgs e) 
  32.         { 
  33.             Binding bindA = new Binding("Value") { Source = this.sliderA }; 
  34.             Binding bindR = new Binding("Value") { Source = this.sliderR }; 
  35.             Binding bindG = new Binding("Value") { Source = this.sliderG }; 
  36.             Binding bindB = new Binding("Value") { Source = this.sliderB }; 
  37.  
  38.             MultiBinding mb = new MultiBinding() { Mode = BindingMode.OneWay }; 
  39.             mb.Bindings.Add(bindA); 
  40.             mb.Bindings.Add(bindR); 
  41.             mb.Bindings.Add(bindG); 
  42.             mb.Bindings.Add(bindB); 
  43.  
  44.             mb.Converter = new DoublesToBrushConverter(); 
  45.             this.rectColor.SetBinding(Grid.BackgroundProperty, mb); 
  46.         } 
  47.     } 


 

这样感觉顺眼多了,回头想想,问题主要出在Color的A、R、G、B属性“不能绑定”。

感觉这种问题应该有别的解决方法,因为可以“绑定资源”的。

想了想,未果。

决定先放在这里。用winform去做我没完成的XXX管理程序。哈哈。

示例下载

 

转自:http://blog.csdn.net/nanqi0506/article/details/7193659

posted @ 2012-09-03 11:57  therockthe  阅读(319)  评论(1)    收藏  举报