首页 > 编程语言 >C# WPF 测试直接绑定依赖属性双向传递是没有问题的

C# WPF 测试直接绑定依赖属性双向传递是没有问题的

时间:2022-11-20 20:23:03浏览次数:64  
标签:DependencyProperty C# 绑定 System MainWindow Windows using WPF public

续这篇测试:https://www.cnblogs.com/huvjie/p/16909290.html

测试页面:

<Window x:Class="MyWPFSimple3.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:MyWPFSimple3"
        mc:Ignorable="d"
        Title="MainWindow" Height="170" Width="300">
    <Grid>
        <StackPanel HorizontalAlignment="Center" Margin="5">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="姓名:" VerticalAlignment="Center"/>
                <TextBox BorderBrush="Black" Height="30" Width="150" VerticalContentAlignment="Center"
                         Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}, Path=PersonName}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0 2 0 0">
                <TextBlock Text="年龄:" VerticalAlignment="Center"/>
                <TextBox BorderBrush="Black" Height="30" Width="150" VerticalContentAlignment="Center" 
                          Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}, Path=PersonAge}" />
            </StackPanel>
            <Button x:Name="btn_ChangeInfo" Click="btn_ChangeInfo_Click" Content="Change Info" Width="186" Margin="0 5 0 0"/>
            <Button x:Name="btn_ShowInfo" Click="btn_ShowInfo_Click"  Content="Show Info" Width="186" Margin="0 5 0 0"/>
        </StackPanel>
    </Grid>
</Window>

BehindCode:

using System;
using System.Collections.Generic;
using System.ComponentModel;
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 MyWPFSimple3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public string PersonName
        {
            get
            {
                return (string)GetValue(PersonNameProperty);
            }
            set
            {
                SetValue(PersonNameProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for PersonName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PersonNameProperty =
            DependencyProperty.Register("PersonName", typeof(string), typeof(MainWindow), new PropertyMetadata("悟空"));

        public int PersonAge
        {
            get
            {
                return (int)GetValue(PerAgeProperty);
            }
            set
            {
                SetValue(PerAgeProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for PerAge.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PerAgeProperty =
            DependencyProperty.Register("PersonAge", typeof(int), typeof(MainWindow), new PropertyMetadata(500));

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private void btn_ChangeInfo_Click(object sender, RoutedEventArgs e)
        {
            PersonName = "玄奘";
            PersonAge = 25;
        }

        private void btn_ShowInfo_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show($"姓名:{PersonName}\r\n年龄:{PersonAge}\r\n");
        }
    }
}

标签:DependencyProperty,C#,绑定,System,MainWindow,Windows,using,WPF,public
From: https://www.cnblogs.com/huvjie/p/16909417.html

相关文章

  • MySQL、Oracle、SQL Server时间类型字段
    关系型数据库时间类型字段MySQL(以及对应格式)SQLServer(以及对应格式)Oracletime(HH-MM-SS)time(12:35:29.1234567)date(YYYY-MM-DDHH:MM:SS)date(YYYY-MM-DD......
  • SpringBoot日志框架LogBack
    logback记录器的属性依赖SpringBoot天生集成了logback日志框架,因此想要输出日志,根本不用再引入多余的依赖日志文件说明appender-为日志输出目的地,log4提供的ap......
  • 线程与GC
    托管线程在GC期间从是否可以访问托管堆上的对象来说有两种模式1.抢占模式,,不可以访问托管堆上的对象2.合作模式,,可以自由访问托管堆上的对象托管线程在GC期间会有两种模......
  • P4125 [WC2012]记忆中的水杉树
    P4125[WC2012]记忆中的水杉树给定\(n\)个平面上不相交的线段,规定将线段按照水平竖直的\(4\)个方向移动至无穷远,若没有碰到任何其他线段则为合法。要求完成\(2\)......
  • MFC插入对话框
    方法一:  方法二:         添加后的对话框:         ......
  • SAP UI5 SmartTable 控件本地运行时进行 Excel 导出的单步调试
    点击SmartTable控件生成的表格控件的ExporttoExcel时,遇到如下错误消息:Thefollowingerrorhasoccurredduringexport:Unexpectedserverresponse:SmartT......
  • Zabbix监控Tomcat
      zabbix是通过javageteway及JMX对Tomcat来进行监控的,并也可以实现自定义模板的导入及应用,模拟实际生产环境中的Tomcat性能等其他参数的图形监测显示。1、准备Tomcat服......
  • HttpWebResponse 四种accept-encoding解析(gzip, deflate, br,identity【转】
    varhwrs=(HttpWebResponse)hwr.GetResponse()if(hwrs.ContentEncoding.ToLower().Contains("gzip")){outhtml=newStream......
  • AtCoder Beignner Contest 278
    D给定一个长度为\(n\)的序列,有如下三种操作:把所有的数全部修改为\(x\)把第\(i\)个数加\(x\)输出第\(i\)个数的值不难发现,每次一操作会覆盖之前的所有操作,所......
  • K8s---【KubeSphere部署nacos集群】
    KubeSphere部署nacos集群1.准备nacos配置文件下载地址:https://github.com/alibaba/nacos/releases/tag/2.0.3注意:下载的是nacos-server-2.0.3.zip,而不是nacos。解压后......