当前位置:网站首页>WPF DataContext 使用
WPF DataContext 使用
2022-06-21 10:27:00 【flysh05】
数据绑定中使用DataContext 数据上下文,DataContext 属性是绑定的默认源,除非你特别声明另一个源。DataContext 属性没有默认源(从一开始就是 null),但是由于 DataContext 是通过控件层次结构向下继承的,因此可以为 Window 本身设置一个 DataContext,然后在所有子控件中使用它。
UI XAML 代码:
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="测试 DataContext" Height="450" Width="800">
<Grid>
<StackPanel Margin="15">
<WrapPanel>
<TextBlock Text="Window title: " />
<TextBox Text="{Binding Title, UpdateSourceTrigger=PropertyChanged}" Width="150" />
</WrapPanel>
<WrapPanel Margin="0,10,0,0">
<TextBlock Text="Window dimensions: " />
<TextBox Text="{Binding Width}" Width="50" />
<TextBlock Text=" x " />
<TextBox Text="{Binding Height}" Width="50" />
</WrapPanel>
</StackPanel>
</Grid>
</Window>
后台绑定:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
}
这个例子的代码隐藏只添加了一行有趣的代码:在标准的 InitalizeComponent() 调用之后,我们将“this”引用分配给 DataContext,它只是告诉窗口我们希望自己成为数据上下文。在 XAML 中,我们使用这个事实来绑定到几个 Window 属性,包括 Title、Width 和 Height。由于窗口有一个 DataContext,它被传递给子控件,我们不必在每个绑定上定义一个源 - 我们只需使用这些值,就好像它们是全局可用的一样。
尝试运行示例并调整窗口大小 - 您将看到尺寸更改立即反映在文本框中。您也可以尝试在第一个文本框中编写不同的标题,但您可能会惊讶地发现此更改并未立即反映。相反,您必须在应用更改之前将焦点移动到另一个控件。

使用 DataContext 属性就像通过控件的层次结构向下设置所有绑定的基础。这为您节省了为每个绑定手动定义源的麻烦,并且一旦您真正开始使用数据绑定,您一定会喜欢节省的时间和输入。
但是,这并不意味着您必须对 Window 中的所有控件使用相同的 DataContext。由于每个控件都有自己的 DataContext 属性,因此您可以轻松打破继承链并使用新值覆盖 DataContext。这允许您做一些事情,例如在窗口上拥有一个全局 DataContext,然后在一个面板上拥有一个单独的表单或类似的东西上一个更本地和特定的 DataContext。
边栏推荐
- 知识点滴 - 什么是加速移动网页(AMP)?
- WCF restful+jwt authentication
- Odd number of characters异常
- Original code, inverse code, complement calculation function applet; C code implementation;
- DSP online upgrade (2) -- design framework of bootloader
- Unable to access gcr IO solutions
- 国金证券开户安全吗?
- Where is the cow in Shannon's information theory?
- js正则-梳理
- 安全百强 中坚力量!美创科技入选《2022年中国数字安全百强报告》
猜你喜欢

character string

js正则-梳理

WCF RestFul+JWT身份验证

Esp8266/esp32 +1.3 "or 0.96" IIC OLED pointer clock

TensorFlow,危!抛弃者正是谷歌自己

并发底层原理:线程、资源共享、volatile 关键字

The more AI evolves, the more it resembles the human brain! Meta found the "prefrontal cortex" of the machine. AI scholars and neuroscientists were surprised

为什么 C# 访问 null 字段会抛异常?

西电AI专业排名超清北,南大蝉联全国第一 | 2022软科中国大学专业排名

WCF restful+jwt authentication
随机推荐
The memory allocation of the program, the storage of local const and global const in the system memory, and the perception of pointers~
Answers to mobile application development learning general test questions
Is it safe for Guojin securities to open an account?
Application configuration management, basic principle analysis
Unity vuforia recommended equipment
JWT与Session的比较
New programmers optimize a line of code on Monday and are discouraged on Wednesday?
2. MySQL index creation method and its optimization
一行代码加速 sklearn 运算上千倍
Audio and video synchronization knowledge points you must pay attention to:
燎原之势 阿里云数据库“百城聚力”助中小企业数智化转型
Positive results of enx-101 clinical 1b study published by English topics
金融机构抢滩“数字员工”;保险APP适老化服务评测框架发布
获取配置文件properties中的数据
Es composite query workload evaluation
2. MySQL index optimization
Will the thunderstorm of Celsius be the "Lehman moment" in the field of encryption?
知识点滴 - 什么是加速移动网页(AMP)?
support vector machine
给电脑加装固态