WPF数据绑定

WPF中的数据绑定是一个强大的机制,它允许将用户界面(UI)元素与后端数据源动态连接。这种连接方式使得UI元素的显示值与数据源的值保持同步,当数据源的值发生变化时,UI元素会自动更新,反之亦然。

数据绑定的类型

ElementName 依据Name相互绑定的
RelativeSource 相对于本身或者父元素
ltemSouce 绑定到集合元素
DataContext 多种不同值绑定

ElementName实例:

将textbox的文本内容绑定到button上(当textbox文本发生变化时,button内容也会跟着变化)
ElementName :  确定绑定的元素
path : 绑定的属性

<StackPanel Orientation="Vertical">
    <TextBox Height="30"
             Width="100"
             BorderBrush="Black" Name="t"></TextBox>
    <Button Width="100"
            Height="40"
            Content="{Binding ElementName=t,Path=Text}"></Button>
</StackPanel>

RelativeSource实例:

属性介绍 :

Mode : 设置绑定的元素是本身还是父元素 (值 :  Self本身  FindAncestor祖先)

AncestorType: 设置绑定父元素的类型

Path: 绑定的属性

<StackPanel Margin="0,100,0,0">
    <Button Height="40"
            FontSize="15"
            Content="{Binding RelativeSource={RelativeSource Mode=Self},Path=Height}" />
    <Button Height="40"
            FontSize="15"
            Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=StackPanel},Path=Margin}" />
</StackPanel>

ltemSouce实例:

第一步: 设置标签

<ListBox Name="l3"
         Width="200"
         Height="100"
         ItemsSource="{Binding}">
</ListBox>

第二步: 设置数据并且进行绑定

ObservableCollection:表示一个动态数据收集,该集合在添加或删除项或刷新整个列表时提供通知。

ObservableCollection<string> list = new ObservableCollection<string>();
list.Add("李四");
list.Add("张三");
list.Add("王五");
this.l3.DataContext = list;

DataContext实例:

INotifyPropertyChanged :是一个接口,它用于在属性值发生更改时通知 WPF UI 元素。当一个类实现了 INotifyPropertyChanged 接口,并且在属性值发生更改时触发 PropertyChanged 事件时,WPF 可以自动更新绑定到这些属性的 UI 元素。

第一步:设置标签

<Grid Name="g"
      DataContext="{Binding}">
    <Button Width="100" Height="40" Content="{Binding Path=One}" ></Button>
    <Button Width="100" Height="40" Content="{Binding Path=Two}"></Button>
</Grid>

第二步:创建模型类 并且进行数据绑定

//创建数据模型类并添加INotifyPropertyChanged接口
public class A : INotifyPropertyChanged
{
    public string One { get; set; }
    public string Two { get; set; }
    // 实现INotifyPropertyChanged这个接口的PropertyChanged属性
    // PropertyChanged类型是委托函数
    public event PropertyChangedEventHandler? PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        // 当属性修改的时候触发PropertyChanged事件,紧跟着调用该函数
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

// 在cs中整理数据并且绑定
this.g.DataContext = new A(){One = "我是One", Two = "我是two" };

相关推荐

  1. WPF数据

    2024-04-12 21:12:01       37 阅读
  2. WPF —— Menu数据实例

    2024-04-12 21:12:01       37 阅读
  3. wpf ComboBox数据及变更事件

    2024-04-12 21:12:01       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 21:12:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 21:12:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 21:12:01       82 阅读
  4. Python语言-面向对象

    2024-04-12 21:12:01       91 阅读

热门阅读

  1. 解锁ChatGPT的论文写作技巧,让学术之路更顺畅

    2024-04-12 21:12:01       52 阅读
  2. 【回溯】Leetcode 78. 子集【中等】

    2024-04-12 21:12:01       36 阅读
  3. 日志埋点功能

    2024-04-12 21:12:01       35 阅读
  4. 【无标题】

    2024-04-12 21:12:01       34 阅读
  5. 深度学习的发展历史与关键技术

    2024-04-12 21:12:01       40 阅读
  6. [C++] 小游戏 斗破苍穹 2.10.1 版本 zty出品

    2024-04-12 21:12:01       36 阅读
  7. LeetCode //C - 540. Single Element in a Sorted Array

    2024-04-12 21:12:01       33 阅读
  8. 最大连通块

    2024-04-12 21:12:01       31 阅读
  9. nginx访问控制

    2024-04-12 21:12:01       37 阅读
  10. docker版postgresql数据库主从配置

    2024-04-12 21:12:01       45 阅读
  11. LeetCode hoot100-22

    2024-04-12 21:12:01       36 阅读
  12. 汽车的基本结构有哪些?

    2024-04-12 21:12:01       77 阅读
  13. Centos7 部署Zabbix6.0 LTS

    2024-04-12 21:12:01       40 阅读