【问题标题】:wpf binding text inside usercontrol text box from main window text box来自主窗口文本框的用户控件文本框内的 wpf 绑定文本
【发布时间】:2014-01-26 02:26:17
【问题描述】:

主窗口

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

希望在从 fbSearchBox 键入时在 txtdynamicText 中显示文本。请帮助

【问题讨论】:

    标签: c# asp.net wpf silverlight mvvm


    【解决方案1】:

    您需要一些可以绑定到的用户控件中的属性。
    向用户控件添加依赖属性:

        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}}}"/>
    

    现在您可以将FacebookAutoComplete.DynamicText 绑定到来自MainWindow 的TextBox:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 2011-03-29
      • 2011-01-23
      • 1970-01-01
      • 2012-11-24
      • 2017-04-17
      • 1970-01-01
      相关资源
      最近更新 更多