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

编程入门 行业动态 更新时间:2024-10-17 05:31:22

WPF中ElementName与RelativeSource绑定的<a href=https://www.elefans.com/category/jswz/34/1750038.html style=局限性以及对策"/>

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

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

<Window x:Class="Test_01.MainWindow"xmlns=""xmlns:x=""xmlns:d=""xmlns:mc=""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=""xmlns:x=""xmlns:d=""xmlns:mc=""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; }}
}

更多推荐

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

本文发布于:2023-11-15 18:10:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603949.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:局限性   绑定   对策   WPF   ElementName

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!