当前位置: 首页 > news >正文

WPF中ElementName与RelativeSource绑定的局限性以及对策

完全来源于十月的寒流,感谢大佬讲解

<Window x:Class="Test_01.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:Test_01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><StackPanel><TextBlock Text="{Binding ElementName=txt, Path=Text, StringFormat='Input: {0}'}" FontSize="30"></TextBlock><TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=StackPanel}, Path=Children[2].Text, StringFormat='Input: {0}'}" FontSize="30"></TextBlock><TextBox x:Name="txt" FontSize="30"></TextBox></StackPanel>
</Window>

RelativeSource解析不到

<Window x:Class="Test_01.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:Test_01"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded"><StackPanel x:Name="Spanel"><TextBlock Text="{Binding ElementName=txt, Path=Text, StringFormat='Input: {0}'}" FontSize="30"></TextBlock><TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=StackPanel}, Path=Children[2].Text, StringFormat='Input: {0}'}" FontSize="30"></TextBlock><!--<TextBox x:Name="txt" FontSize="30"></TextBox>--></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 Test_01
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void Window_Loaded(object sender, RoutedEventArgs e){var box = new TextBox{Name = "txt",};this.RegisterName(box.Name, box);Spanel.Children.Add(box);}}public class MainWindowViewModel{public string Message { get; set; }}
}
http://www.lryc.cn/news/223663.html

相关文章:

  • 基于PHP语言的会员系统搭建(Docker版)
  • 文件改名:一次性解决文件名混乱,批量重命名技巧
  • app自动化测试——capability 配置参数解析
  • 数仓面经大框架
  • C++ explicit关键字的作用
  • 排序算法之-冒泡
  • 【微服务】API治理发展历史与未来趋势
  • TikTok shop美国小店适合哪些人做?附常见运营问题解答
  • [OS]11.9.2023 中断
  • unity笔记
  • 二十、泛型(5)
  • Oracle递归查询树形数据
  • pycharm pro v2023.2.4(Python开发)
  • 武汉某母婴用品公司 - 集简云连接ERP和营销系统,实现库存管理的自动化
  • AD9371 Crossbar
  • 设计模式 -- 责任链模式(Chain of Responsibility Pattern)
  • 开源:特殊的垄断
  • linux安装oracle client解决cx_Oracle.DatabaseError: DPI-1047
  • 工业园区一般用多大规格的电表?
  • Debian12换镜像源
  • deeplog中输出某个 event 的概率
  • 流媒体中relay和forword的区别
  • git创建本地分支并提交到远程
  • Selenium定位元素的方法css和xpath的区别
  • 【目标检测】SSD损失函数详解
  • C【整数正序分解】
  • 基于springboot实现高校党务平台管理系统【项目源码】计算机毕业设计
  • Day24力扣打卡
  • 5G-A 商用加速,赋能工业互联网
  • 代码随想录day2