Sudesh
Sudesh

Reputation: 61

wpf binding text inside usercontrol text box from main window text box

main window

<TextBox x:Name="fbSearchBox" Margin="69,10,298,11" TextWrapping="Wrap" BorderBrush=" x:Null}"/>
<local:FacebookAutoComplete "want to set text from here of fbSearchBox" />

user control

<UserControl x:Class="ZuneWithCtrls.Pages.Facebook.Home.FacebookAutoComplete"
         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" Width="431" Height="49"
         x:Name="uc">
<TextBlock x:Name="txtDynamicText"/>

</UserControl>

want to show text in txtdynamicText when type from fbSearchBox.. Plz help

Upvotes: 0

Views: 5462

Answers (1)

Dennis
Dennis

Reputation: 37770

You need some property in user control you can bind to.
Add a dependency property to your user control:

    public static readonly DependencyProperty DynamicTextProperty =
       DependencyProperty.Register(
          "DynamicText",
          typeof(string),
          typeof(FacebookAutoComplete),
          new FrameworkPropertyMetadata(null));

    public string DynamicText
    {
        get { return (string)GetValue(DynamicTextProperty); }
        set { SetValue(DynamicTextProperty, value); }
    }

Bind a nested control inside user control to this dependency property:

<TextBlock x:Name="txtDynamicText" 
           Text="{Binding Path=DynamicText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"/>

Now you can bind FacebookAutoComplete.DynamicText to the TextBox from MainWindow:

<local:FacebookAutoComplete DynamicText="{Binding Text, ElementName=fbSearchBox, UpdateSourceTrigger=PropertyChanged}"/>

Upvotes: 4

Related Questions