首页 > 其他分享 >WPF中的Binding的常见知识点与技巧

WPF中的Binding的常见知识点与技巧

时间:2023-11-08 10:32:25浏览次数:31  
标签:知识点 Windows 绑定 Binding System using WPF public 属性

在XAML中,可以绑定到许多不同类型的数据源和属性。以下是一些可以绑定的常见数据源和属性:

属性:可以绑定到对象的属性,例如控件的Text、Visibility、IsEnabled等属性。

集合:可以绑定到集合数据,如List、ObservableCollection、Array等。在绑定到集合时,还可以使用索引器绑定到特定项。

静态资源:可以使用x:Static引用静态字段或属性,如常量、枚举、静态类的属性等。

数据上下文:在WPF和其他XAML框架中,每个元素都有一个数据上下文,可以在此上下文中绑定到其父元素的属性或继承的数据上下文的属性。

数据模型:可以绑定到MVVM(Model-View-ViewModel)模式中的数据模型,通常是一个实现INotifyPropertyChanged接口的类。

XML和JSON数据:可以绑定到XML或JSON数据,使用XMLDataProvider或ObjectDataProvider等。

资源字典:可以绑定到资源字典中的资源,如样式、模板、图像等。

命令:可以使用Command绑定到自定义命令,以在用户交互时执行操作。

视觉状态:可以绑定到不同的视觉状态,以根据应用程序的当前状态更改UI。

动画:可以绑定到动画属性,以在动画执行时更改UI元素的属性。

这些只是一些常见的绑定数据源,实际上,XAML绑定是非常灵活的,可以将其用于几乎任何具有属性或数据的地方。数据绑定是XAML中非常强大的特性,可以用于创建动态、交互式和可扩展的用户界面。

一、Source

<Window x:Class="BindingTest.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:BindingTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.DataContext>
        <local:MainWindowViewModel></local:MainWindowViewModel>
    </Window.DataContext>

    <Window.Resources>
    </Window.Resources>

    <StackPanel>
        <TextBlock Text="{Binding}" FontSize="30"></TextBlock>
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;

namespace BindingTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MainWindowViewModel
    {
        public string Message => "this is test";

        public override string ToString()
        {
            return "hello world"; 
        }
    }
}

后台实现binding

<Window x:Class="BindingTest.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:BindingTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">

    <Window.DataContext>
        <local:MainWindowViewModel></local:MainWindowViewModel>
    </Window.DataContext>

    <Window.Resources>
    </Window.Resources>

    <StackPanel>
        <TextBlock x:Name="tbl" FontSize="30"></TextBlock>
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;

namespace BindingTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var binding = new Binding
            {
                Path = new PropertyPath("Message"),
                Mode = BindingMode.TwoWay,
            };
            BindingOperations.SetBinding(tbl, TextBlock.TextProperty, binding);
        }
    }

    public class MainWindowViewModel
    {
        private string message = "this is test";
        public string Message
        {
            get { return message; }
            set { message = value; }
        }
    }
}
这是尝试在TextBlock元素的Source属性上使用DynamicResource,但是Source属性不是DependencyProperty,因此无法直接使用DynamicResource。只能在DependencyObject的DependencyProperty上使用DynamicResource。



使用属性、静态属性、常量资源

标签:知识点,Windows,绑定,Binding,System,using,WPF,public,属性
From: https://blog.51cto.com/u_15319978/8245937

相关文章

  • WPF多UI线程
       internalclassSpashWindowManager{privatestaticSpashWindow_spashWindow;privatestaticThreadthread;publicstaticvoidShow(){thread=newThread(()=>{_spashW......
  • [WPF]浅析资源引用(pack URI)
    WPF中我们引用资源时常常提到一个概念:packURI,这是WPF标识和引用资源最常见的方式,但不是唯一的方式。本文将介绍WPF中引用资源的几种方式,并回顾一下packURI标识引用在不同位置的资源文件的写法。WPF中引用资源的几种方式WPF中使用URI标识和加载位于各种位置的文件,包括当前程序......
  • WPF仿VS TreeView
    [TemplatePart(Name="PART_Content",Type=typeof(ToggleButton))][TemplatePart(Name="Expander",Type=typeof(Panel))]publicclassOTreeViewItem:TreeViewItem{Panel?partContent;ToggleButton?pa......
  • WPF开发的小巧、美观桌面快捷工具GeekDesk开源项目--极客桌面
    今天给大家推荐一个基于WPF开发的,专门为程序员定制的桌面快捷工具。项目简介这是基于.Net+WPF开发的,一个小巧、UI美观的快捷工具。此项目发布以来就受到大家的喜欢,代码结构清晰非常适合用来学习。项目还在持续迭代中,有部分小问题,用来学习、体验完全没问题。作者一直在迭代升级......
  • ServiceAccount ClusterRole ClusterRoleBinding
    RoleBinding的作用是把ServiceAccount绑定到Role上,Role规定了可以对资源做的操作,把ServiceAccount绑定到Role上就表示拿到这个ServiceAccount的程序就有了权限对资源做这些操作。当然,有ClusterRole和ClusterRoleBinding,ClusterRole可以在包括所有NameSpce和集群级别的资源或非资......
  • 《MySQL》复习必刷知识点
    1.数据库SQL语言的缩写DDL:数据定义语言DataDefinitionLanguage,定义语言就是定义关系模式、删除关系、修改关系模式以及创建数据库中的各种对象,比如表、聚簇、索引、视图、函数、存储过程和触发器关键字包括:Create,Alter,Drop,TruncateDML:数据操纵语言全称是DataManipulationLa......
  • 界面控件DevExpress WPF PDF Viewer,更快实现应用的PDF文档浏览
    DevExpressWPFPDFViewer控件可以轻松地直接在Windows应用程序中显示PDF文档,而无需在最终用户的机器上安装外部PDF查看器。P.S:DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应......
  • wpf 记一次诡异的PreviewMouseLeftButtonDown 无法触发问题
    1、原始代码<Grid><i:Interaction.Triggers><i:EventTriggerEventName="PreviewMouseLeftButtonDown">......
  • 职场小白必备知识点-DHCP协议介绍
    1.DHCP协议简介DHCP,全称是DynamicHostConfigurationProtocolo中文名为动态主机配置协议,它的前身是BOOTP,它工作在OSI的应用层,是一种帮助计算机从指定的DHCP服务器获取它们的配置信息的自举协议。DHCP使用客户端/服务器模式,请求配置信息的计算机叫做DHCP客户端,而提供信息的叫做D......
  • 网络知识点
    知识点补充 is-is   vlan        ......