<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否不能确定了)

(运行时)

点击+,两个数据控件绑定的值会++--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吧!

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

转载于:https://www.cnblogs.com/SongSharp/archive/2011/06/17/2083606.html

WPF Tile=” 变量 UI 双向绑定”x:Class=Problem/相关推荐

  1. 【WPF】WPF DataGrid List数据源 双向绑定通知机制之ObservableCollection使用以及MultiBinding 的应用...

    以下代码实现了DataGrid的简单绑定List数据源 重点要提一下的是,绑定List数据源,但是不能直接用List.比如下面的代码,使用List<GridItem>只能实现数据修改的绑定 ...

  2. Vue2 MVVM 双向绑定(数据劫持+发布者-订阅者模式)

    参考文献:https://www.cnblogs.com/libin-1/p/6893712.html https://juejin.im/post/5b2f0769e51d45589f46949e ...

  3. DataBinding使用指南(一)DataBinding基本使用,双向绑定,ListView RecycleView使用

    databing使用指南 简介 简单使用 双向绑定 ListView.RecycleView中的使用 . ListView ListView 中数据的简单展示 数据源改变后数据更新方式 . Recyc ...

  4. vue的双向绑定原理及实现

    前言 使用vue也好有一段时间了,虽然对其双向绑定原理也有了解个大概,但也没好好探究下其原理实现,所以这次特意花了几晚时间查阅资料和阅读相关源码,自己也实现一个简单版vue的双向绑定版本,先上个成果图 ...

  5. Vue自定义组件--输入框的双向绑定--自动切换输入法的录入框

    最简单的Input的封装 HtmlInput.vue <template><div><input :value="value" @input=&quo ...

  6. 记录vue的双向绑定原理及实现

    这里写自定义目录标题 思路分析 实现过程 1.实现一个Observer 2.实现Watcher 此文章是学习以为大神____chen的 <vue的双向绑定原理及实现> vue数据双向绑定是 ...

  7. Vue是怎么实现数据双向绑定的

    vue数据双向绑定原理 vue数据双向绑定是通过数据劫持结合发布者-订阅者模式的方式来实现的,那么vue是如果进行数据劫持的,我们可以先来看一下通过控制台输出一个定义在vue初始化数据上的对象是个什么 ...

  8. Qt - UI数据双向绑定简易实现

    文章目录 前言 原理 Qt 实现思路 源码 效果 一些想法 参考鸣谢 乍暖还寒时候,与上班提醒互道早安. 前言 自从前端大火了以后,UI数据双向绑定的ui框架愈发流行.作为前菜鸡安卓开发,我也是最近才 ...

  9. 解决WPF ListView虚拟化ListViewItem的IsSelected属性MVVM双向绑定bool值后出现的绑定错乱的问题

    在一次使用WPF ListView的时候,我发现在MVVM模式下在ListView开启虚拟化的情况下,将ListViewItem的IsSelected属性与一个对应的布尔值进行双向绑定. 文档目录结构 ...

最新文章

  1. c primer plus--数据和C(第3章)--习题
  2. 云服务器可以安装操作系统么,云服务器安装操作系统吗
  3. rabbitmq 入门demo
  4. 红米android版本,微信红米低版本下载
  5. java中集合类的转换_JAVA-常用集合类型转换例子(基础必备)
  6. css td 溢出改为省略号
  7. java语言与c++语言相比_最突出的特点是_Java 语言与C++语言相比,最突出的特点是( )。_学小易找答案...
  8. HTML 内容不能被选择,不能被复制
  9. tfs 文件系统部署_使用SQL Server数据工具和使用自定义工作流文件的TFS部署到多个数据库
  10. Linux终端进程后台运行与前后台切换
  11. numpy 在机器学习中 常用函数总结
  12. 回溯2--部分全排列
  13. 拓端tecdat|使用R语言进行时间序列(arima,指数平滑)分析
  14. time clock getrusage clock_gettime gettimeofday timespec_get 对比
  15. 从零开始学习 ASP.NET MVC 1.0 (四) View/Model 全解 【转】
  16. PLC编程需注意的地方
  17. python外部库matlab_python调用MATLAB库绘制直方图
  18. 高并发衡量指标及解决方案
  19. AutoRun类型分析
  20. 时间管理技巧(清理一波文件 感觉这个还是有用的 )

热门文章

  1. laravel 分词搜索匹配度_DSSM文本匹配模型在苏宁商品语义召回上的应用
  2. html 根作用域,AngularJS入门教程之Scope(作用域)
  3. Apache Web Server - httpd 的虚拟主机的配置
  4. 每天一道LeetCode-----找到第k个排列
  5. 每天一道LeetCode----位运算实现加减乘除四则运算
  6. arm汇编解析—qnnpack卷积实现
  7. javaweb使用 数据库连接池 DBCP,实现对数据库驱动使用优化,多个 action共用一个数据库连接
  8. 【代码规范】google开源c\c++项目代码规范
  9. 《Linux内核设计与实现》读书笔记(七)- 中断处理
  10. python模仿windows文件管理_python – 在Windows中显示文件的资源管理器属性对话框...