2014-01-08 45 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

谢谢丹尼斯,这对我很有用.... –