如何从已添加到窗口的UserControl中的元素绑定到窗口中的元素 [英] How do I Bind to elements in a Window from an element in a UserControl that has been added to the window

查看:76
本文介绍了如何从已添加到窗口的UserControl中的元素绑定到窗口中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从已添加到窗口的UserControl中的元素绑定到主窗口中的元素



How do I Bind to elements in the Main Window from an element in a UserControl that has been added to the window

<Window x:Class="WpfApplication1.MainWindow"
        xmlns:local="clr-namespace:WpfApplication1">
    <Grid x:Name="LayoutRootGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock x:Name="FirstNameTextBlock" Grid.Row="0"
                 Text="John"/>
        <TextBox x:Name="LastNameTextBlock" Grid.Row="1"
                 Text="Smith"/>
        <local:SecondControl x:Name="SecondControl" Grid.Row="2"/>
    </Grid>
</Window>





自定义用户控件如下所示





The custom user control is like below

<UserControl x:Class="WpfApplication1.SecondControl">
    <StackPanel Background="CadetBlue">
        <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"
                   Text="{Binding ElementName=FirstNameTextBlock, Path=Text}" />
        <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"
                   Text="{Binding ElementName=LastNameTextBlock, Path=Text}" />
    </StackPanel>
</UserControl>





上面的绑定设置不起作用......我做错了什么?



刚开始学习WPF ...



感谢您的帮助!



The above binding setup did not work...What am I doing wrong?

Just started learning WPF...

Thanks for your help!

推荐答案

你不能直接这样做。

窗口 SecondControl 是两个不同的上下文(WPF XAML NameScope), ElementName 受NameScope限制。

你可以引用元素只有在相应的NameScope中注册了代码

MSDN:Binding.ElementName [ ^ ]



所以,我看到两种可能性:

1. 如果 FirstNameTextBlock / LastNameTextBlock cont 窗口的rols是 NOT 硬编码,而是数据绑定到窗口 DataContext ,然后 SecondControl 可以使用相同的绑定描述,因为它继承了 DataContext 来自窗口

You can't do this directly.
The Window and SecondControl are two different contexts (WPF XAML NameScope) and the ElementName is limited by NameScope.
"You can refer to elements in code only if they are registered to the appropriate NameScope"
MSDN: Binding.ElementName[^]

So, I see two possibilities:
1. If the FirstNameTextBlock/LastNameTextBlock controls of the Window are NOT "hardcoded", but are instead data bound to the Window's DataContext, then the SecondControl can use the same binding descriptions, since it has inherited the DataContext from the Window:
<Window x:Class="WpfApplication1.MainWindow"

        xmlns:local="clr-namespace:WpfApplication1">
    <Grid x:Name="LayoutRootGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock x:Name="FirstNameTextBlock" Grid.Row="0"

                 Text="{Binding Path=FirstName}"/>
        <TextBox x:Name="LastNameTextBlock" Grid.Row="1"

                 Text="{Binding Path=LastName}"/>
        <local:SecondControl x:Name="SecondControl" Grid.Row="2"/>
    </Grid>
</Window>

<UserControl x:Class="WpfApplication1.SecondControl">
    <StackPanel Background="CadetBlue">
        <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"

                   Text="{Binding Path=FirstName}" />
        <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"

                   Text="{Binding Path=LastName}" />
    </StackPanel>
</UserControl>



2.(可能更好)将 SecondControl 包含 DependencyProperties 可以在窗口中使用 SecondControl 的绑定


2. (and probably better) would be to have the SecondControl contain DependencyProperties that can take the binding where the SecondControl is used in the Window:

<Window x:Class="WpfApplication1.MainWindow"

        xmlns:local="clr-namespace:WpfApplication1">
    <Grid x:Name="LayoutRootGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="100"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock x:Name="FirstNameTextBlock" Grid.Row="0"

                 Text="John"/>
        <TextBox x:Name="LastNameTextBlock" Grid.Row="1"

                 Text="Smith"/>
        <local:SecondControl x:Name="SecondControl" Grid.Row="2"

                             FirstName="{Binding ElementName=FirstNameTextBlock, Path=Text}"

                             LastName="{Binding ElementName=LastNameTextBlock, Path=Text}" />
    </Grid>
</Window>

<UserControl x:Class="WpfApplication1.SecondControl" x:Name="SecondControl" >
    <StackPanel Background="CadetBlue">
        <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"

                   Text="{Binding ElementName=SecondControl, Path=FirstName}" />
        <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top"

                   Text="{Binding ElementName=SecondControl, Path=LastName}" />
    </StackPanel>
</UserControl>



SecondControl.xaml。 cs


In SecondControl.xaml.cs:

public static readonly DependencyProperty FirstNameProperty =
    DependencyProperty.Register("FirstName", typeof(string), typeof(SecondControl), new UIPropertyMetadata(string.Empty));

public string FirstName
{
  get { return (string)GetValue(FirstNameProperty); }
  set { SetValue(FirstNameProperty, value); }
}

public static readonly DependencyProperty LastNameProperty =
    DependencyProperty.Register("LastName", typeof(string), typeof(SecondControl), new UIPropertyMetadata(string.Empty));

public string LastName
{
  get { return (string)GetValue(LastNameProperty); }
  set { SetValue(LastNameProperty, value); }
}


这篇关于如何从已添加到窗口的UserControl中的元素绑定到窗口中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆