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

相关推荐
wyh要好好学习20 分钟前
C# WPF 记录DataGrid的表头顺序,下次打开界面时应用到表格中
开发语言·c#·wpf
lgcgkCQ15 小时前
任务调度中心-XXL-JOB使用详解
java·wpf·定时任务·任务调度
Vicky&James16 小时前
英雄联盟客户端项目:从跨平台Uno Platform到Win UI3的转换只需要30分钟
github·wpf·跨平台·英雄联盟·winui·unoplatform
就是有点傻1 天前
WPF中如何使用区域导航
wpf
她说彩礼65万1 天前
WPF程序设置单例启动(互斥体)
wpf
就是有点傻1 天前
WPF中Prism框架中 IContainerExtension 和 IRegionManager的作用
wpf
月落.1 天前
WPF中MVVM工具包 CommunityToolkit.Mvvm
wpf·mvvm
月落.1 天前
WPF Prism框架
wpf·prism
Crazy Struggle2 天前
.NET 8.0 通用管理平台,支持模块化、WinForms 和 WPF
vue·wpf·winform·.net 8.0·通用权限管理
呆萌很3 天前
HCIP-HarmonyOS Application Developer 习题(二十)
华为·wpf·harmonyos