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

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

csharp 复制代码
<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解析不到

csharp 复制代码
<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>
csharp 复制代码
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; }
    }
}

namescope详细了解下

相关推荐
lingxiao1688814 小时前
智慧停车场(SmartParking)
c#·自动化·wpf
战族狼魂18 小时前
上位机软件开发完整学习路线与项目实战指南
单片机·c#·wpf
500841 天前
昇腾 CANN 的五层架构,到底分了哪五层
java·人工智能·分布式·架构·ocr·wpf
醉颜凉2 天前
ZooKeeper Zxid 与 Epoch 深度解析:分布式事务的时空坐标
分布式·zookeeper·wpf
500842 天前
HCCL 集合通信编程:多卡协同的正确姿势
java·flutter·性能优化·electron·wpf
500842 天前
用 Ascend CL 从零写一个推理程序
人工智能·深度学习·机器学习·性能优化·wpf
彦为君2 天前
Spring定时任务开发指南(动态实现)
java·开发语言·后端·python·spring·wpf
她说彩礼65万2 天前
WPF中Style和ControlTemplate的触发器有什么不同
wpf
玖笙&3 天前
✨WPF编程基础【3.3】:容器控件(附源码)
c++·wpf·visual studio
500843 天前
GE 怎么做算子融合
分布式·架构·开源·wpf