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详细了解下

相关推荐
小白18 小时前
WPF自定义Dialog模板,内容用不同的Page填充
wpf
Crazy Struggle1 天前
C# + WPF 音频播放器 界面优雅,体验良好
c#·wpf·音频播放器·本地播放器
James.TCG2 天前
WPF动画
wpf
He BianGu2 天前
笔记:简要介绍WPF中FormattedText是什么,主要有什么功能
笔记·c#·wpf
脚步的影子2 天前
.NET 6.0 + WPF 使用 Prism 框架实现导航
.net·wpf
jyl_sh2 天前
Ribbon (WPF)
ribbon·wpf·client·桌面程序开发·c/s客户端
wo63704312 天前
[Visual Stuidio 2022使用技巧]2.配置及常用快捷键
c#·.net·wpf
小黄人软件3 天前
wpf 字符串 与 变量名或函数名 相互转化:反射
wpf
界面开发小八哥3 天前
DevExpress WPF中文教程:如何解决排序、过滤遇到的常见问题?(二)
.net·wpf·界面控件·devexpress·ui开发
Vae_Mars3 天前
WPF中图片的宫格显示
wpf