[英]How to multibind a parent element property with child usercontrol elements properties
I need to multibind Text property of a text box in my window with text properties of textboxes in child usercontrol.BeloW is the code I tried,but didnt work as expected. 我需要将窗口中文本框的Text属性与子usercontrol中文本框的文本属性进行多重绑定。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>
Any help will be highly appreciated. 任何帮助将不胜感激。 Thanks 谢谢
You cant access txtF and txtL by name because the TextBoxes inside the UserControl are in a different namescope. 您不能按名称访问txtF和txtL,因为UserControl中的TextBox位于不同的名称范围中。 Check this link for more information on WPF naming scopes. 检查此链接以获取有关WPF命名范围的更多信息。
The easiest way to do it would probably be to add a 'TextCombined' property to your usercontrol, set it when one of the TextBoxes changes its text and bind to it in your window: 最简单的方法可能是在用户控件中添加一个'TextCombined'属性,在其中一个TextBoxes更改其文本并将其绑定到您的窗口时对其进行设置:
Code-Behind of your UserControl: 您的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: 视窗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>
The UserControl XAML does not need to be changed. 不需要更改UserControl XAML。 Hope this works for you. 希望这对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.