当前位置:网站首页>WPF入门项目必知必会-初步了解数据绑定 binding
WPF入门项目必知必会-初步了解数据绑定 binding
2022-08-01 05:07:00 【济南医疗小程序状元】
XAML代码界面
<Window x:Class="WpfApp1.Data数据绑定.Windowdata"
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.Data数据绑定"
xmlns:s="clr-namespace:WpfApp1.Entity"
mc:Ignorable="d"
Title="Windowdata" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
<Grid.Resources>
<!-- 一 静态资源类加载方式
这是个资源字典
key 为 MydataSource
value 为 Mydata类 C#
-->
<s:Mydata x:Key="mydataSource"/>
</Grid.Resources>
<!--数据上下文绑定 控件1-->
<Grid.DataContext>
<Binding Source="{StaticResource mydataSource}"/>
</Grid.DataContext>
<!--1 按钮进行了一个绑定,都是有迹可循的。
2 因为在这边 button 是Grid 的子元素,所以 他Grid绑定的数据源, 可以分享给旗下的button。
-->
<Button Background="{Binding Path=ColorName}"
Width="{Binding Path=Width}" Height="30">
</Button>
<!--第二种 窗体加载的时候,触发绑定方式
直接在后台数据绑定 控件-->
<Label Name="myLabel" Content="{Binding Path=Title}" FontSize="50" FontWeight="Bold">
</Label>
</Grid>
</Window>
entity 文件夹中的mydata 类
基础类定义,用法重复,一部分重复了,一部分更新了。
更新不同的是用法的不同,本质没有太大改变,变化。
using System;
using System.Collections.Generic;
using System.Text;
namespace WpfApp1.Entity
{
public class Mydata
{
private string colorName = "green";
private int width = 500;
//private string title
/// <summary>
/// 这样子colorName的属性的默认值就是 "red”
/// </summary>
// 属性首字母大写啊
public string ColorName { get => colorName; set => colorName = value; }
public int Width { get => width; set => width = value; }
public string Title { get; set; }
}
}
交互逻辑类
using System;
using System.Collections.Generic;
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.Shapes;
using WpfApp1.Entity; // 需要引入必要的命名空间啊
namespace WpfApp1.Data数据绑定
{
/// <summary>
/// Windowdata.xaml 的交互逻辑
/// </summary>
public partial class Windowdata : Window
{
public Windowdata()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// 数据绑定 进行. 窗体加载的时候,触发绑定
myLabel.DataContext = new Mydata() { Title = "我是个标题" };
}
}
}
运行项目
友情关键提示:加载项目地方,要修改为个人的文件夹路径,否则启动会报错的!
效果演示

边栏推荐
- RSA主要攻击方法
- 【云原生之kubernetes实战】kubernetes集群的检测工具——popeye
- 风险策略调优中重要的三步分析法
- typescript21 - Comparison of Interfaces and Type Aliases
- PMP 相关方管理必背总结
- typescript26 - literal types
- Excuse me, only primary key columns can be queried using sql in table storage. Does ots sql not support non-primary keys?
- pytorch、tensorflow对比学习—功能组件(激活函数、模型层、损失函数)
- USB3.0:VL817Q7-C0的LAYOUT指南(二)
- (2022 Niu Ke Duo School IV) K-NIO's Sword (Thinking)
猜你喜欢

Power button (LeetCode) 212. The word search II (2022.07.31)

typescript21 - Comparison of Interfaces and Type Aliases

DL-31/6电流继电器

「以云为核,无感极速」顶象第五代验证码

初识shell脚本

Code Interview Guide for Programmers CD15 Generating an Array of Windowed Maximums

USB3.0:VL817Q7-C0的LAYOUT指南(三)

PaddleX部署推理模型和GUI界面测试结果不一致的解决方法

可持久化线段树

Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation
随机推荐
Selenium:元素等待
vim配置+ctag像source insight一样方便阅读代码
Error: AttributeError: module 'matplotlib' has no attribute 'figure'
挑战52天背完小猪佩奇(第01天)
Li Chi's work and life summary in July 2022
pytorch、tensorflow对比学习—功能组件(优化器、评估指标、Module管理)
UE4 rays flashed from mouse position detection
MySQL-数据定义语言-DDLdatebase define language
在互联网时代,有诸多「互联网+」模式的诞生
【云原生之kubernetes实战】kubernetes集群的检测工具——popeye
MySQL实践总结-
PMP 80个输入输出总结
pytroch、tensorflow对比学习—搭建模型范式(构建模型方法、训练模型范式)
Selenium:弹窗处理
万字逐行解析与实现Transformer,并进行德译英实战(一)
pytorch、tensorflow对比学习—张量
Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记
Seleniu:元素常用操作
DL-31/6电流继电器
Logitech Mouse Experience Record