如何将父元素属性与子用户控件元素属性多重绑定

时间:2019-03-19 05:48:15

标签: c# wpf xaml

我需要将窗口中文本框的Text属性与子用户控件中文本框的文本属性进行多重绑定。BeloW是我尝试过的代码,但没有按预期工作。

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:uc="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
   <Grid>
      <Grid.RowDefinitions>
          <RowDefinition/>
          <RowDefinition/>
      </Grid.RowDefinitions>
      <TextBox Width="150" HorizontalAlignment="Center" 
       VerticalAlignment="Center">
      <TextBox.Text>
            <MultiBinding StringFormat=" {0} {1}}">
                <Binding Path="txtF.Text" RelativeSource="{RelativeSource 
                   Mode=FindAncestor, AncestorType={x:Type UserControl}}"/>
                <Binding Path="txtl.Text"  RelativeSource="{RelativeSource 
                   Mode=FindAncestor, AncestorType={x:Type UserControl}}"/>
            </MultiBinding>
      </TextBox.Text>
    </TextBox>
   <uc:UserControl1 x:Name="SomeUC"  Grid.Row="1" 
       HorizontalAlignment="Center" VerticalAlignment="Top"/>
   </Grid>
 </Window>
 <UserControl x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup- 
         compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
 <Grid>
    <StackPanel Orientation="Horizontal">
        <TextBox x:Name="txtF" Width="120" Height="20" Margin="5"/>
        <TextBox  x:Name="txtL" Width="120" Height="20"/>
    </StackPanel>
</Grid>
</UserControl>

任何帮助将不胜感激。 谢谢

1 个答案:

答案 0 :(得分:2)

您不能按名称访问txtF和txtL,因为UserControl中的TextBox位于不同的名称范围中。检查this link以获得有关WPF命名范围的更多信息。

最简单的方法可能是在用户控件中添加一个'TextCombined'属性,在其中一个TextBoxes更改其文本并将其绑定到您的窗口时对其进行设置:

UserControl的代码隐藏:

public partial class UserControl1 : UserControl
{
    public static readonly DependencyProperty TextCombinedProperty =
        DependencyProperty.Register("TextCombined", typeof(string),
            typeof(UserControl1), new PropertyMetadata(String.Empty));

    public string TextCombined
    {
        get { return (string)GetValue(TextCombinedProperty); }
        set { SetValue(TextCombinedProperty, value); }
    }

    public UserControl1()
    {
        InitializeComponent();
        txtF.TextChanged += OnTextFieldTextChanged;
        txtL.TextChanged += OnTextFieldTextChanged;
    }

    private void OnTextFieldTextChanged(object _, TextChangedEventArgs __)
    {
        SetCurrentValue(TextCombinedProperty, $"{txtF.Text} {txtL.Text}");
    }
}

Window XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBox Width="150" 
             HorizontalAlignment="Center" 
             VerticalAlignment="Center"
             Text="{Binding ElementName=SomeUC, Path=TextCombined}" />

    <local:UserControl1 x:Name="SomeUC"
                        Grid.Row="1" 
                        HorizontalAlignment="Center"
                        VerticalAlignment="Top"/>
</Grid>

不需要更改UserControl XAML。希望这对您有用。