首页 > 其他分享 >WPF中bind使用

WPF中bind使用

时间:2023-12-29 09:34:51浏览次数:27  
标签:string bind PropertyChangedEventArgs TestViewMode static 使用 new WPF public

1、例TextBox的text关联类的属性

1.1、类的创建

    class TestViewMode : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public string show;//显示

        public string Show
        {
            get { return show; }
            set
            {
                show = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Show"));
            }
        }
  }

1.2、XAML代码

<TextBox x:Name="textBox" Text="{Binding  Path=Show, Mode=TwoWay}" />

1.3、关联上下文

    public partial class MainWindow : Window
    {
        TestViewMode vm =new TestViewMode();
        public MainWindow()                 
        {          
           this.DataContext = vm;
        }
    }

2、类中静态变量在关联主窗口中的TextBox、在另外的页面中调用这个静态变量

2.1、类的创建

    class TestViewMode : INotifyPropertyChanged
    {
        // 定义静态属性值变化事件 
        public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

        private static void OnStaticPropertyChanged([CallerMemberName] string propertyName = null)
        {
            OnStaticPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        private static void OnStaticPropertyChanged(PropertyChangedEventArgs e)
        {
            StaticPropertyChanged?.Invoke(null, e);
        }
        private static string property1;
        // 定义静态属性
        public static string Property1
        {
            get => property1;
            set
            {
                property1 = value;
                OnStaticPropertyChanged();
            }
        }
    }

2.2、XAML代码

<TextBox x:Name="textBox" Text="{Binding  Path=Property1, Mode=TwoWay}" />

2.3、关联上下文

    public partial class MainWindow : Window
    {
        TestViewMode vm =new TestViewMode();
        public MainWindow()                 
        {          
           this.DataContext = vm;
        }
    }

2.4 在Page页面直接调用

TestViewMode.Property1 = "xxxxxx";

 

标签:string,bind,PropertyChangedEventArgs,TestViewMode,static,使用,new,WPF,public
From: https://www.cnblogs.com/suwencjp/p/17934050.html

相关文章

  • 使用Numpy实现手写数字识别
    1概要  用Python语言在只使用Numpy库的前提下,完成一个全连接网络的搭建和训练。2实现代码参考:https://github.com/binisnull/ann2.1环境设置  创建Python3.8.16的虚拟环境,激活并执行python-mpipinstallnumpy==1.18.5tensorflow-gpu==2.3.0Pillowmatplot......
  • 自己写的mapper.xml如何使用mybatis-plus的自带分页?
    在MyBatis-Plus中,使用自带的分页功能非常简单。首先,确保你的mapper.xml文件中定义了需要的SQL语句,并在相应的mapper接口中使用IPage类型的参数进行分页。接下来,使用Page类来包装查询条件,并调用Mapper接口的分页方法。首先,假设你的mapper.xml中有类似如下的查询语句:<!--在mapper.xm......
  • Java Spring Boot Mybatis-Plus 的简单使用
    此文主要基于官网case整理,如需了解更多详情,请移步官网。环境:SpringBoot:3.1.6JDK:17MySQL:5.7数据准备主要是MySQL建库建表,插入一些数据。建库:CREATEDATABASEmybatis_demo;建表:DROPTABLEIFEXISTS`user`;CREATETABLE`user`(idBIGINTNOTNULLCOMME......
  • 在Linux平台安装使用Anaconda
    下载在https://repo.anaconda.com/archive/https://repo.anaconda.com/archive/找到一个合适的版本,右键复制链接然后wget它:wgethttps://repo.anaconda.com/archive/Anaconda3-2023.07-2-Linux-x86_64.sh安装添加执行权限:chmod+xAnaconda3-2023.07-2-Linux-x86_64.sh运......
  • 定时器原理及使用
    一、引入在进行并发编程时,有时候会需要定时功能,比如监控某个GO程是否会运行过长时间、定时打印日志等等。GO标准库中的定时器主要有两种:Timer定时器、Ticker定时器。Timer计时器使用一次后,就失效了,需要Reset()才能再次生效。而Ticker计时器会一直生效。二、Timer定时器1)实现原......
  • Vb.net 使用Webview2显示pdf文件
    使用webview2显示PDF文件需要wvliulanqi--Webview2控件的 AwaitWv2.EnsureCoreWebView2Async函数来启动浏览器否则会报错注意Div的宽度高度PrivateSubButton1_ClickAsync(senderAsObject,eAsEventArgs)HandlesButton1.ClickDimstrPathAs......
  • mrml 使用中的一些问题
    mrml对于mjml的兼容还是很不错的,目前是一些问题问题mjmlversion问题这个属于早期版本的问题了,目前使用方法已经不包含此参数了 <mjmlversion="3.3.3">应该去掉version其他配置参数mrml的实现与mjml的配置参数基本一致,有几个参数我们需要......
  • KEYSIGHT LCR使用visa通信的几个问题
    C#中使用visa网口与LCR通信1.在Keysight官网上下载IOLibrariessuite并安装,将C:\ProgramFiles\IVIFoundation\VISA\Win64\ktvisa\include\visa32.csC:\ProgramFiles\IVIFoundation\VISA\Win64\agvisa\agbin\visa32.dll拷贝到自己工程中,此dll为非托管,属性设置资......
  • Go 泛型之明确使用时机与泛型实现原理
    目录一、引入二、何时适合使用泛型?场景一:编写通用数据结构时场景二:函数操作的是Go原生的容器类型时场景三:不同类型实现一些方法的逻辑相同时三、Go泛型实现原理Stenciling方案Dictionaries方案Go最终采用的方案:GCShapeStenciling方案四、泛型对执行效率的影响五、小结一......
  • Apipost-Helper使用流程
    Apipost-Helper是由Apipost推出的IDEA插件,写完接口可以进行快速调试,且支持搜索接口、根据method跳转接口,还支持生成标准的API文档,注意:这些操作都可以在代码编辑器内独立完成,非常好用!这里给大家介绍一下Apipost-Helper的安装和使用安装在IDEA编辑器插件中心输入Apipost搜索安装:Api......