现在的位置: 首页 > 综合 > 正文

2012年04月24日 ⁄ 综合 ⁄ 共 4989字 ⁄ 字号 评论关闭
<TextBox Name="txtAbout" Tag="废话">
    最近才开始正规的学习WPF,以前只是激动,观摩,欣赏,不敢亵玩焉!我这个从游戏而进入编程的顽童,对于Dx自然热心,看过学过,却没有真正用过,曾经YY过如果Dx能用来做开发,那界面一定牛!结果就“我佩服”了。最看了三章《WPF揭秘》,为自己曾经学的走马观花而懊悔不已。WPF与Winform的概念已经有很大的不同,更像html+Winform的编程方式。看了两天书,刚刚开始实践数据绑定,结果摔了一个跟斗,昨晚熬夜也没折腾出来,baidu,google,MSDN…发现很多技术博客内容比较少,所以写出来,希望大家不要走看弯路!
这里最大的感慨,学技术,特别是编程技术,切不可急躁,不求甚解!!! </TextBox>

内容提要:

1.实现INotifyPropertyChanged (MSDN)接口,UI控件的双向数据绑定

简单的控件数据绑定并不是难题,但我想实习“后台”数据改变的同时,UI控件与数据一起改变,却出现了只能绑定初始值,后台实时修改后的值无法在UI界面显现。那肯定是代码有问题,程序是死的,只有代码是血淋淋的~~

2.转换器IValueConverter(MSDN)接口的实现双向数据绑定

因为想要写一个简单的ColorDialog练练绑定,结果悲剧的是我只能得到初始颜色,执行代码改变了颜色Ui却无视我一直改变的量~~

内容:

环境:win7+vs2010 + framework 3.5 (sp1否不能确定了)

image(运行时)

点击+,两个数据控件绑定的值会++--10,按钮的背景色的R,G,B会改变,这里要多按基础。程序只是示例,代码短不优化

MainWindow.xaml:

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:WpfTest"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <src:ColorBrushConverter x:Key="colorBrushConverter"/>
    </Window.Resources>
    <Grid Name="grid">
        <!--Slider与TextBox同时绑定了A_int变量-->
        <Slider Height="23" HorizontalAlignment="Left"  Value="{Binding Path=A_int,Mode=TwoWay}"   Margin="12,12,0,0" Name="slider1" VerticalAlignment="Top" Width="248" Maximum="100" />

        <TextBox Height="23" HorizontalAlignment="Right" Text="{Binding Path=A_int,Mode=TwoWay}"  Margin="0,12,117,0" Name="textBox1" VerticalAlignment="Top" Width="120" />

        <!--两个Button可以改变A_int变量的值,同时也改变A_Color变量的值-->
        <Button Content="+" Height="25" HorizontalAlignment="Right" Margin="0,53,290,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" Background="{Binding Path=A_Color, Converter={StaticResource colorBrushConverter}}" />

        <Button Content="-" Height="23" HorizontalAlignment="Left" Margin="40,53,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" Background="{Binding Path=A_Color, Converter={StaticResource colorBrushConverter}}" />
    </Grid>
</Window>

MainWindow.xaml.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.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public date d=new date();
        public MainWindow()
        {           
            InitializeComponent();
            d.A_int=10;
            d.A_Color=Colors.Black;
            grid.DataContext=d;
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            d.A_int += 10;
            d.A_Color = Color.FromArgb((byte)255, (byte)(d.A_Color.R + 10), (byte)(d.A_Color.G + 10), (byte)(d.A_Color.B + 1));
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            d.A_int -= 10;
            d.A_Color = Color.FromArgb((byte)255, (byte)(d.A_Color.R - 10), (byte)(d.A_Color.G - 10), (byte)(d.A_Color.B - 1));
        }

    }

    public class date  : INotifyPropertyChanged//实现接口,详细表述MSDN
    { 
        public event PropertyChangedEventHandler PropertyChanged;
        private int a_int;
        public int A_int
        {
            get { return a_int; }
            set
            {
                if (value == a_int) return;
                a_int = value;
                this.FirePropertyChanged("A_int");
            }
        }
        public Color a_Color;
        public Color A_Color
        {
            get { return a_Color; }
            set
            {
                if (value == a_Color) return;
                a_Color = value;
                this.FirePropertyChanged("A_Color");
            }
        }
        private void FirePropertyChanged(string propertyName) 
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }       
    }
    /// <summary>
    /// SolidColorBrush与color的转化
    /// </summary>
    [ValueConversion(typeof(Color), typeof(SolidColorBrush))]
    public class ColorBrushConverter : IValueConverter//实现接口,详细表述MSDN
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Color color = (Color)value;
            return new SolidColorBrush(color);
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

}

关于实现INotifyPropertyChanged (MSDN)接口,数据绑定

<Slider Height="23" HorizontalAlignment="Left"  Value="{Binding Path=A_int}"   Margin="12,12,0,0" Name="slider1" VerticalAlignment="Top" Width="248" Maximum="100" />

<TextBox Height="23" HorizontalAlignment="Right" Text="{Binding Path=A_int,Mode=TwoWay}"  Margin="0,12,117,0" Name="textBox1" VerticalAlignment="Top" Width="120" />

这是两个被绑定的控件,我修改了Slider 的绑定代码

Value="{Binding Path=A_int}"

这里我改了代码运行,结果是Slider 成功的双向绑定了数值,可能TwoWay是Mode的默认值

 

转换器IValueConverter(MSDN)接口的实现双向数据绑定

这里查了很多资料,实验了很多代码,但多数都失败了,更失败的是我没有总结失败的原因,没有错误的代码,只要写错代码的人。

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:WpfTest"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <src:ColorBrushConverter x:Key="colorBrushConverter"/>
    </Window.Resources>

这里是定义转换器,可能查到的wpf的代码太老,很多的Resources定义的代码都无效,后面使用Background="{Binding Path=A_Color, Converter={StaticResource colorBrushConverter}}"就可以顺利绑定的,同样也没有Mode=TwoWay…
xmlns:src="clr-namespace:WpfTest"
<src:ColorBrushConverter x:Key="colorBrushConverter"/>
这里,我的vs总是报错,我奇怪了很久。哎,这次真不是我的错了,更新一下,有时更新也继续报错,那么,按F5吧!


上班时间到挺久了,继续码代码…

抱歉!评论已关闭.