How do I bind an element that was inherited from a User Control?

1

I've assembled a User Control that will be a screen template for me to implement other screens and avoid repetition of layout components. It contains a TextBox txb Search.

<UserControl x:Class="SistemaComercial.Presentation.WPF.Views.Lists.UserConstrols.ucListPage"
             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" 
             xmlns:local="clr-namespace:SistemaComercial.Presentation.WPF.Views.Lists.UserConstrols"
             xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
             mc:Ignorable="d" 
             d:DesignHeight="768" d:DesignWidth="1024">
    <Grid>         
    <Grid Name="Principal">
        <Grid.RowDefinitions>
            <RowDefinition Height="70"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="100"/>
        </Grid.RowDefinitions>

        <Border Background="#FF444444" Grid.Row="0">
            <DockPanel VerticalAlignment="Stretch">
                <Canvas>
                    <TextBox controls:TextBoxHelper.ClearTextButton="True" Name="txtPesquisar" Width="376" Canvas.Top="20" Canvas.Left="525" FontSize="18" DockPanel.Dock="Right" controls:TextBoxHelper.Watermark="Pesquisar informações do cliente">
                    </TextBox>                        
                </Canvas>
            </DockPanel>
        </Border>
        <Border Grid.Row="2"/>
    </Grid>
</Grid>

In my page of clients I made the link with the User Control, but then hit the doubt: How do I give a Binding in the txtSearch.text that is on my page, if it is just an inherited component of the User control?

<Page x:Class="SistemaComercial.Presentation.WPF.Views.Lists.ClienteListPage"
      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" 
      xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
      xmlns:uc="clr-namespace:SistemaComercial.Presentation.WPF.Views.Lists.UserConstrols"
      xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
      xmlns:local="clr-namespace:SistemaComercial.Presentation.WPF.Views.Lists"
      xmlns:viewModels="clr-namespace:SistemaComercial.Presentation.WPF.ViewModels"
      mc:Ignorable="d" 
      d:DesignHeight="768" d:DesignWidth="1024"
      Title="ClienteListPage">

    <Grid>
        <uc:ucListPage (txtPesquisar.text = {Binding Path=Pesquisa}")/>
    </Grid>

</Page>
    
asked by anonymous 10.10.2017 / 00:56

1 answer

1

You can use RelativeSource to access the UserControl context.

<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, 
                    AncestorType={x:Type UserControl}}, Path=Foo}" />

From here you can change the Path to the desired one.

10.10.2017 / 04:31