首页 > 其他分享 >wpf binding例子

wpf binding例子

时间:2022-11-28 13:12:59浏览次数:34  
标签:控件 PropertyChanged xmlns binding 例子 窗体 wpf public

1、binding源为非控件,而是C#类/实例

窗体(V)代码,

<Window x:Class="WpfBindingApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfBindingApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock Width="160" Height="50" Text="年龄:" Margin="37,192,603,193" FontSize="20"/>
<TextBox Width="160" Height="50" x:Name="tb333" Text="{Binding Age}" Margin="134,173,506,212"/>
<TextBlock Width="160" Height="50" Text="姓名:" Margin="37,266,603,119" FontSize="20"/>
<TextBox Width="160" Height="50" x:Name="tb444" Margin="134,247,506,138" Text="{Binding person.Name}" />

<Button x:Name="btn111" Width="80" Height="60" Click="btn111_Click" Margin="360,299,360,76"/>
</Grid>
</Window>

 

 

 

 

C#后台(VM)代码,这里为了好看,可以把person类改名为xxViewModel

 

public class Person: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
if (PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}

}

 

public partial class MainWindow : Window, INotifyPropertyChanged  //窗体底层cs代码
{
public event PropertyChangedEventHandler PropertyChanged;

public Person person { get; set; } = new Person();  // 1)要使用{ get; set; },令其作为属性,而不能是字段!

private int _age;
public int Age  //这里也可以看作是(M)代码,直接把(binding源)属性写在同一个(即binding接收对象/UI所在的)窗体/页面类
{
get { return _age; }
set
{
_age = value;
if (PropertyChanged!= null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
}
}
}


public MainWindow()
{
InitializeComponent();
_age = 10;
person.Name = "Tom";

this.DataContext = this;  //2)使用DataContext作为binding源 。注意窗体/页面类和binding接收控件是不一样,比如说binding接收控件绑定自身,并不是指它所在的窗体/页面类,而就是它这个控件自身。
}

private void btn111_Click(object sender, RoutedEventArgs e)
{
Age += 1;
person.Name += "+2" ;
}
}

 

标签:控件,PropertyChanged,xmlns,binding,例子,窗体,wpf,public
From: https://www.cnblogs.com/castlewu/p/16931919.html

相关文章

  • WPF中实现MVVM
    MVVM(Model-View-ViewModel):Model指的是后台传递的数据,View指的是所看到的页面。ViewModel是mvvm模式的核心,它是连接view和model的桥梁。它有两个方向:一是将模型转化成视......
  • Spring mvc整合hibernate例子
    企业级项目实战(带源码)地址:[url]http://zz563143188.iteye.com/blog/1825168[/url]收集五年的开发资料及源码下载地址:[url]http://pan.baidu.com/......
  • ASP.NET Core教程-Model Binding(模型绑定)
    更新记录转载请注明出处:2022年11月27日发布。2022年11月25日从笔记迁移到博客。模型绑定是什么模型绑定是指:使用来自HTTP请求的值来创建.NET对象的过程。模型绑......
  • 【WPF】布局 测量和排序
    概览     一、WPF布局原则WinForm的布局是采用基于坐标的方式,当窗口内容发生变化时,里面的控件不会随之动态调整,用户体验不够好。而WPF采用了基于流的布局方......
  • Prism框架(一)——概述Prism框架的设计目的是用来帮助构建丰富、灵活、易维护的WPF和Si.
    SiPrism框架(一)——概述Prism框架的设计目的是用来帮助构建丰富、灵活、易维护的WPF和SiPrism框架(一)——概述Prism框架的设计目的是用来帮助构建丰富、灵活......
  • WPF 样式
    .引言样式(Style),主要是用来让元素或内容呈现一定外观的属性。WPF中的样式的作用,就像Web中的CSS一样,为界面上的元素定制外观,以提供更好的用户界面。在WPF应用程序中,通过控......
  • WPF中的事件及冒泡事件和隧道事件(预览事件)的区别
    WPF的采取了路由事件机制,这样事件可以在可视树上层级传递。要知道XAML中控件都是由很多其他元素组合而成,比如我们单击了Button内部的TextBlock元素,Button依然可以可......
  • wpf中UserControl制作
    上篇文章里面说到UserControl主要是现有控件的组合。组合是好组合啊,我随便拖几个控件往页面里面一放,比如我弄个TextBox和一个button往页面里面一方,起个名字就是UserControl......
  • aioredis 2.0大不一样,1.3得例子都运行不了。
    官方文档 https://aioredis.readthedocs.io/en/v2.0.1/migration/   迁移到v2.0¶概括¶aioredisv2.0现在是一个完全兼容的asyncio-redis -py的本机实现......
  • wpf 报错 System.ArgumentException:““{0}”不是 Visual 或 Visual3D。”
    通常发生于 DataGrid 的自定义模版列(DataGridTemplateColumn)中.仔细检查是否使用了 <TextBlock>+<Run> 来显示绑定的内容,并且没有指定其绑定模式为单向(OneWay).......