WPF 精修篇 数据绑定到对象

数据绑定到对象
首先 我们需要一个对象
-
public class Preson
-
{
-
private string name;
-
-
public string Name
-
{
-
get { return name; }
-
set { name = value; }
-
}
-
private string address;
-
-
public string Address
-
{
-
get { return address; }
-
set { address = value; }
-
}
-
private int age;
-
-
public int Age
-
{
-
get { return age; }
-
set { age = value; }
-
}
-
}
设置依赖属性绑定数据上下文
-
public MainWindow()
-
{
-
InitializeComponent();
-
Preson = new Preson() { Name = "慧哥", Address = "大连", Age = 30 };
-
DataContext = Preson;
-
}
-
-
-
-
public Preson Preson
-
{
-
get { return (Preson)GetValue(PresonProperty); }
-
set { SetValue(PresonProperty, value); }
-
}
-
-
// Using a DependencyProperty as the backing store for Preson. This enables animation, styling, binding, etc...
-
public static readonly DependencyProperty PresonProperty =
-
DependencyProperty.Register("Preson", typeof(Preson), typeof(MainWindow), new PropertyMetadata(null));
-
-
<Grid HorizontalAlignment="Right" Width="517">
-
<Grid.ColumnDefinitions>
-
<ColumnDefinition Width="109*"/>
-
<ColumnDefinition Width="112*"/>
-
<ColumnDefinition Width="94*"/>
-
<ColumnDefinition Width="202*"/>
-
</Grid.ColumnDefinitions>
-
<Grid.RowDefinitions>
-
<RowDefinition Height="24*"/>
-
<RowDefinition Height="25*"/>
-
<RowDefinition Height="25*"/>
-
<RowDefinition Height="86*"/>
-
</Grid.RowDefinitions>
-
<TextBox HorizontalAlignment="Left" Height="23" Margin="101,27,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{Binding Age,Mode=TwoWay}" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" />
-
<TextBlock HorizontalAlignment="Left" Margin="41,22,0,0" TextWrapping="Wrap" Text="名字" VerticalAlignment="Top"/>
-
<TextBlock HorizontalAlignment="Left" Margin="79,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding Name}" Width="30"/>
-
<TextBlock HorizontalAlignment="Left" Margin="10,22,0,0" TextWrapping="Wrap" Text="address" VerticalAlignment="Top" Grid.Column="1"/>
-
<TextBlock Margin="63.667,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding Address}" Grid.Column="1"/>
-
<TextBlock HorizontalAlignment="Left" Margin="20,22,0,0" TextWrapping="Wrap" Text="age" VerticalAlignment="Top" Grid.Column="2"/>
-
<TextBlock Margin="57,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding Age}" Grid.Column="2"/>
-
<TextBox HorizontalAlignment="Left" Height="23" Margin="0,17,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" Grid.Column="2" />
-
</Grid>
前台WPF绑定
这里也可以用 INotifyPropertyChanged 也可以
集合使用ObservableCollenction<对象>
WPF 精修篇 数据绑定到对象的更多相关文章
- WPF 精修篇 数据绑定 更新通知
原文:WPF 精修篇 数据绑定 更新通知 开始更新一点有意思的了 首先 数据绑定 其中之一 Element 绑定 看例子 <Window x:Class="WpfApplicatio ...
- WPF 精修篇 依赖属性
原文:WPF 精修篇 依赖属性 依赖属性使用场景 1. 希望可在样式中设置属性. 2. 希望属性支持数据绑定. 3. 希望可使用动态资源引用设置属性. 4. 希望从元素树中的父元素自动继承属性值. 5 ...
- WPF 精修篇 用户控件
原文:WPF 精修篇 用户控件 增加用户控件 数据绑定还是用依赖属性 使用的事件 就委托注册一下 public delegate void ButtonClick(object b,EventArgs ...
- WPF 精修篇 路径动画
原文:WPF 精修篇 路径动画 路径动画 是让一个对象围绕指定Path 的运动路径 进行移动的动画 举栗子 路径动画 使用 Blend 来设置 是十分简单的 首先用工具 笔 点出一条线 新建一个圆形 ...
- 【值转换器】 WPF中Image数据绑定Icon对象
原文:[值转换器] WPF中Image数据绑定Icon对象 这是原来的代码: <Image Source="{Binding MenuIcon}" ...
- WPF 精修篇 数据触发器
原文:WPF 精修篇 数据触发器 数据触发器 可以使用Binding 来绑定控件 或者数据源 来触发相关动作 举栗子 <Window.Resources> <Style Target ...
- WPF 精修篇 属性触发器
原文:WPF 精修篇 属性触发器 属性触发器是通过 某个条件触发改变属性 通过无代码实现功能 <Style TargetType="{x:Type Label}"> ...
- WPF 精修篇 样式继承
原文:WPF 精修篇 样式继承 这个 是新知识 样式可以继承 <Style x:Key="TextBlockStyleBase" TargetType="{x:Ty ...
- WPF 精修篇 自定义控件
原文:WPF 精修篇 自定义控件 自定义控件 因为没有办法对界面可视化编辑 所以用来很少 现在实现的是 自定义控件的 自定义属性 和自定义方法 用VS 创建自定义控件后 会自动创建 Themes 文件 ...
随机推荐
- spring+cxf No bean named 'cxf' available
最近项目中需要用到webservice,在spring中集成cxf时一直报错: 严重: StandardWrapper.Throwable org.springframework.beans.fact ...
- 12C新功能:在线移动分区 (Doc ID 1584032.1)
12C New Feature: Online Move Partition (Doc ID 1584032.1) APPLIES TO: Oracle Database - Enterprise E ...
- 32(1).层次聚类---AGNES
层次聚类hierarchical clustering 试图在不同层次上对数据集进行划分,从而形成树形的聚类结构. 一. AGNES AGglomerative NESting:AGNES是一种常用的 ...
- ADB常用命令(一)
转自:https://blog.csdn.net/qq_26552691/article/details/81348222 一.操作前请确认电脑上已配置好ADB环境.可在CMD命令行输入adb,如果出 ...
- angularjs中directive指令与component组件有什么区别?
壹 ❀ 引 我在前面花了两篇博客分别系统化介绍了angularjs中的directive指令与component组件,当然directive也能实现组件这点毋庸置疑.在了解完两者后,即便我们知道co ...
- AJAX-CORS 跨域
1.CORS就是一套AJAX跨域问题的解决方案. 2.CORS的原理: CORS定义一种跨域访问的机制,可以让AJAX实现跨域访问. 3.CORS浏览器支持情况: Chrome 3+ Firefox ...
- ej3-0开端
开始 编码多年,总有一些最佳实践,Java也是,比如设计模式,比如Effective Java 3 (ej3) . 设计模式先后看过<大话设计模式>,<HeadFirst 设计模式& ...
- vue获取后端数据放在created还是mounted方法里面?
问题提出: 我们知道一般vue使用ajax或者axios来获取后端数据,并且好像放在created里面和mounted里面都可以获取数据并正确渲染.那么放在created里面和mounted里面有什么 ...
- idea从mapper接口跳到xml文件
- Django之web框架原理
Web框架原理 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 先写一个 原始的web框架 imp ...