当前位置:网站首页>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 = "我是个标题" };
}
}
}
运行项目
友情关键提示:加载项目地方,要修改为个人的文件夹路径,否则启动会报错的!
效果演示
边栏推荐
- 4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
- Li Chi's work and life summary in July 2022
- FFmpeg 搭建本地屏幕录制环境
- (2022牛客多校四)H-Wall Builder II(思维)
- 备战金九银十,如何顺利通过互联网大厂Android的笔面试?
- typescript23-元组
- 华为Android开发面试后得出的面试秘诀
- 程序员代码面试指南 CD15 生成窗口最大值数组
- LeetCode 231. 2 的幂
- 高数 | 【重积分】线面积分880例题
猜你喜欢
随机推荐
Robot_Framework:关键字
(2022牛客多校四)D-Jobs (Easy Version)(三维前缀或)
Selenium:上传、下载文件
Error: AttributeError: module 'matplotlib' has no attribute 'figure'
状态压缩dp
LeetCode 9. 回文数
基于Arduino制作非接触式测温仪
6-23漏洞利用-postgresql代码执行利用
typescript23-tuple
typescript25-类型断言
MySQL-DML语言-数据库操作语言-insert-update-delete-truncate
冲刺金九银十,Android开发面试(内含面试资料|面试题|源码)
零序电流继电器器JL-8C-12-2-2
数组问题之《下一个排列》、《旋转图像》以及二分查找之《搜索二维矩阵》
挑战52天背完小猪佩奇(第01天)
y83. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (14)
typescript23-元组
万字逐行解析与实现Transformer,并进行德译英实战(二)
(2022牛客多校四)A-Task Computing (排序+动态规划)
[MySQL] 多表查询