当前位置:网站首页>WPF DataContext 使用(2)
WPF DataContext 使用(2)
2022-06-22 03:59:00 【flysh05】
1. 使用窗口类的DataContext
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public string MyProperty {
get; set; } = "Test ";
public int MyInt {
get; set; } = 123;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
}
创建窗口2个属性字段,一个字符串属性,一个整型属性。
this.DataContext = this; 窗口界面就使用这个2个属性值的绑定。
窗口创建2个文本,绑定到属性字段。
<Grid>
<StackPanel>
<TextBox FontSize="48" Text="{Binding MyProperty}"/>
<TextBox FontSize="48" Text="{Binding MyInt}"/>
</StackPanel>
</Grid>
效果如下:

2. 创建一个类,将类的实例赋值到DataContext
public class Student
{
public string Name {
get; set; } = "Josh";
public int Id {
get; set; } = 1002;
}
public MainWindow()
{
InitializeComponent();
Student std = new Student();
this.DataContext = std;
}
前台UI代码创建绑定:
<Grid>
<StackPanel>
<TextBox FontSize="48" Text="{Binding Id}"/>
<TextBox FontSize="48" Text="{Binding Name}"/>
</StackPanel>
</Grid>
效果如下:

3. 创建界面子元素的DataContext 属性绑定
再添加一个类:
public class Employee
{
public int Id {
get; set; } = 1001;
public string Name {
get; set; } = "Mike";
}
界面设计绑定:
<Grid>
<StackPanel >
<StackPanel.DataContext>
<local:Employee x:Name="Emp"></local:Employee>
</StackPanel.DataContext>
<TextBox FontSize="48" Text="{Binding Id}"/>
<TextBox FontSize="48" Text="{Binding Name}"/>
<StackPanel>
<StackPanel.DataContext>
<local:Student x:Name="Stu"></local:Student>
</StackPanel.DataContext>
<TextBox FontSize="48" Text="{Binding Id}"/>
<TextBox FontSize="48" Text="{Binding Name}"/>
</StackPanel>
</StackPanel>
</Grid>
效果如下:

当然类的属性值时可以动态变化的,所以绑定的值也动态可变的。
边栏推荐
- php判断当前时间有没有超过几点
- Django 学习--- 模型与数据库操作(二)
- 我在华为度过的 “两辈子”(学习那些在大厂表现优秀的人)
- Introduction to Beifu TwinCAT NC PTP
- Invalid character found in request destination. Valid characters are defined in RFC 7230 and RFC 3986
- It is about one-step creating Yum source cache in Linux
- 顺序表的基本操作
- 如何快速定位bug和编写测试用例?
- Relationship among original code, complement code and inverse code
- [schematic diagram and PCB] design of ultrasonic rangefinder based on single chip microcomputer
猜你喜欢

Tried several report tools, and finally found a report based on Net 6

"Defi moral paradox" behind solend farce

How to write a detailed and professional software function test report

SSM inpatient management system

Insert sort

Online text batch inversion by line tool

Dart异步是怎麼實現

IDEA蓝屏的解决方案
![[BP regression prediction] optimize BP regression prediction based on MATLAB GA (including comparison before optimization) [including Matlab source code 1901]](/img/73/1e4c605991189acc674d85618cf0ef.png)
[BP regression prediction] optimize BP regression prediction based on MATLAB GA (including comparison before optimization) [including Matlab source code 1901]

Shell Sort
随机推荐
图的基本概念
Fluent rendering Principle & detailed explanation of three trees
Relationship among original code, complement code and inverse code
冒泡排序
LeetCode --- 1221. Split a string in balanced strings problem solving Report
Empty, isset and is of PHP_ Null difference
The "two lives" I spent in Huawei (learning from those who performed well in large factories)
How does twitter decentralize? Look at these ten socialfi projects
[Niu Ke's questions -sql big factory interview questions] no1 An audio short video
Shell Sort
Django learning - model and database operation (II)
Sequential implementation of queues
邻接矩阵,邻接表,十字链表,邻接多重表
Dart异步是怎么实现
TCL Huaxing released the world's first 0.016hz ultra-low frequency OLED wearable device screen
CentOS uses composer install to report an error - phpunitphppunit 8
What are the differences between SVN and CVS
Dart异步是怎麼實現
LOCAL=NO
BFs of figure