首页 > 其他分享 >WPF - 001 数据绑定

WPF - 001 数据绑定

时间:2023-07-15 12:22:05浏览次数:28  
标签:代码 绑定 Dependency 001 Path WPF public 属性

数据绑定

WPF数据绑定四大要素:绑定源、绑定源属性、绑定目标、绑定目标属性。

img

绑定源可以是:

  • CLR 对象
  • ADO
  • XML
  • Dependency Object

绑定目标可以是:

  • Dependency Object

能够绑定的属性都是依赖属性,即 Dependency Property,所有 Dependency Property 的值都是 Object -> DenpendencyProperty 类的对象。

数据绑定的方式

在 UI 代码中绑定

采用 {binding ElementName = xxx, Path = xxx} 的方式,将某个元素的某个属性绑定到当前元素的属性上。

例如:将 TextBox 的 Text 属性绑定到 TextBlock 的 Text 属性上。

后台代码:

<StackPanel Margin="10">
        <TextBox Name="txtName" Height="20" Width="200" HorizontalAlignment="Left"></TextBox>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Hello "></TextBlock>
            <TextBlock Text="{Binding ElementName=txtName, Path=Text}"></TextBlock>
        </StackPanel>
    </StackPanel>

img

使用 DataContext 属性

使用控件的 DataContext 可以将另一个控件对象的属性绑定到当前对象的数据上下文中,这样在前台代码中就不需要声明绑定源了,仅需声明绑定源的属性即可,即 {Binding Path=xxx}

后台代码:

public partial class Binding2 : Window
{
    public Binding2()
    {
        InitializeComponent();

        // 设置 txbName 的数据上下文对象
        this.txbName.DataContext = this.txtName;
    }
}

前台代码:

<StackPanel Margin="10">
        <TextBox Name="txtName" Height="20" Width="200" HorizontalAlignment="Left"></TextBox>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Hello "></TextBlock>
            <TextBlock Name ="txbName" Text="{Binding Path=Text}"></TextBlock>
        </StackPanel>
    </StackPanel>

效果一致:
img

在后台代码中绑定

使用 Binding 类进行绑定,首先实例化一个 Binding 对象,包含两个字段 SourcePath,分别表示绑定源和绑定源的属性,然后使用控件的 SetBinding 方法进行属性的绑定。

后台代码:

public partial class Binding3 : Window
{
    public Binding3()
    {
        InitializeComponent();

        BindingData();
    }

    private void BindingData()
    {
        var binding = new Binding
        {
            Source = txtName,
            Path = new PropertyPath("Text"),
        };
        txbName.SetBinding(TextBlock.TextProperty, binding);
    }
}

前台代码:

<StackPanel Margin="10">
    <TextBox Name="txtName" Height="20" Width="200" HorizontalAlignment="Left"></TextBox>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Hello "></TextBlock>
        <TextBlock Name ="txbName"></TextBlock>
    </StackPanel>
</StackPanel>

标签:代码,绑定,Dependency,001,Path,WPF,public,属性
From: https://www.cnblogs.com/snoopy1866/p/17555934.html

相关文章

  • 分库分表建表阶段 绑定表问题要注意
    垂直分表,一个表的字段分散到多个表里面,每个表都存储其中一部分字段,减少io争抢,减少锁表的几率。同时发挥高频数据的操作效率,冷热数据进行分离1、把不常用的字段单独放到一张表里面2.把text,bolb等大字段拆分出来单独放在一张表里面3.经常组合查询的字段单独放到一张表里面垂直......
  • 109.vue双向数据绑定原理
    109.vue双向数据绑定原理?vue通过使用双向数据绑定,来实现了View和Model的同步更新。vue的双向数据绑定主要是通过使用数据劫持和发布订阅者模式来实现的。首先我们通过Object.defineProperty()方法来对Model数据各个属性添加访问器属性,以此来实现数据的劫持,因此当M......
  • WPF 实现 Message 消息提醒控件
    WPF实现Message消息提醒控件控件:Message作者:WPFDevelopersOrg-驚鏵原文链接:https://github.com/WPFDevelopersOrg/WPFDevelopers框架使用.NET4至.NET6;VisualStudio2022;接着上一篇1)新增MessageListBoxItem.cs代码如下:新增了名为MessageTy......
  • 深入浅出WPF——P91把子集集合的元素当Path
    publicpartialclassMainWindow:Window{publicMainWindow(){InitializeComponent();List<Country>countryList=newList<Country>(){newCountry......
  • WPF CheckBox勾选框大小设置
    1、设置CheckBox,FontSize,只有字体发生变化,前面的勾选框太小,可以设置LayoutTransform<StyleTargetType="CheckBox"><SetterProperty="HorizontalAlignment"Value="Left"/><SetterProperty="LayoutTra......
  • WPF TreeView 检测SelectedItem变化的简单方案
    TreeView无法绑定SelectedItem,而又想知道treeview的selecteditem的变化,当然目前有很多方法,我这里简单的提供一个。目前主要思路就是通过处理xaml的TreeViewItem的IsSelected属性来进行绑定。<TreeViewBorderThickness="0"Width="220"......
  • ORA-03001: Unimplemented Feature
    一、创建主键添加online报错ORA-03001:unimplementedfeature二、官网解决办法ORA-03001:UnimplementedFeatureWhenAddingConstraintsWithOnlineClausefrom19.11(DocID2799005.1)1.19.16版本先添加唯一约束,在添加主键;2.在老版本可以使用online添......
  • 001 学习笔记--Access 常用操作
    Access数据库——设计试图,可进行表字段设计Access数据库——双击表,可维护数据常用CRUD帮助方法如下所示:usingSystem.Data;usingSystem.Data.OleDb;namespaceDBHelper{publicstaticclassAccessHelper{//privatestaticstringconnString=Confi......
  • antd from 表单中的key 不能绑定input中的字段 Input.js:207 Uncaught (in promise)
    <Formclass="NewVersion"ref="formRef"name="NewVersion":model="formData"><Spacev-for="(newPg,index)informData.version":key="index"style="dis......
  • WPF获取MainWindows实例
    WPF获取MainWindow实例在其他类中获取MainWindow实例,获取其控件,改变其控件属性等,代码如下:_mainWindow=Application.Current.Windows.Cast<Window>().FirstOrDefault(Window=>WindowisMainWindow)asMainWindow;例如在ConfigureViewModel中改变MainWindow中的某个TextB......