2014-01-08 48 views
0

主窗口中用戶控件的文本框WPF從主窗口文本框中

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

用戶控件內文本裝訂

<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> 

要顯示在txtdynamicText文本時從fbSearchBox類型.. plz幫助

回答

4

您需要可以綁定到的用戶控件中的某些屬性。
依賴屬性添加到您的用戶控件:

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); } 
    } 

綁定用戶控件中嵌套控制此依賴項屬性:

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

現在你可以從MainWindow結合FacebookAutoComplete.DynamicTextTextBox

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

謝謝丹尼斯,這對我很有用.... –