简体   繁体   English

如何将用户控件的文本框中的文本绑定到文本块中?

[英]How to bind the text in a textbox of a usercontrol into a textblock?

I create a user Control textbox and i want to bind the textblock to what i type in the user control's textbox. 我创建了一个用户控件文本框,我想将文本块绑定到我在用户控件文本框中键入的内容。 I try some code, but it doesn't work. 我尝试了一些代码,但是没有用。 Anyone can teach me? 有人可以教我吗? Thanks 谢谢

My userControl TextBox: 我的userControl TextBox:

<Grid  Background="Silver" Style="{StaticResource EntryFieldStyle}"  Width="175" Height="25" Margin="0" >          
    <TextBox Name="watermarkTextBox" Background="Green"   />    
</Grid>

My xaml code: 我的XAML程式码:

<StackPanel Orientation="Horizontal">
      <UserControls:WatermarkTextBox x:Name="usernameArea"/>
      <TextBlock Text="{Binding ElementName=usernameArea Path=watermarkTextBox.Text}"  FontSize="13" Foreground="White"/> 
</StackPanel>

Edit2 : One way to do this is using a dependency property along with implementing INotifyPropertyChanged. Edit2 :执行此操作的一种方法是使用依赖项属性以及实现INotifyPropertyChanged。

What'll happen is that we'll fire a PropertyChangedEvent every time the textbox's text is changed. 将会发生的是,每次文本框的文本更改时,我们都会触发PropertyChangedEvent。 The Window window will subscribe to this event by accessing WatermarkTextBox's WatermarkText dependency property. 窗口窗口将通过访问WatermarkTextBox的WatermarkText依赖项属性来订阅此事件。

Here's how it looks: 外观如下:


WatermarkTextbox.xaml: WatermarkTextbox.xaml:

<TextBox Name="watermarkTextBox" ...
         TextChanged="watermarkTextBox_TextChanged"/>

WatermarkTextbox.xaml.cs: WatermarkTextbox.xaml.cs:

public partial class WatermarkTextBox : UserControl, INotifyPropertyChanged
{
    ...
    public static readonly DependencyProperty WatermarkTextProperty =
        DependencyProperty.Register("WatermarkTextProperty", typeof(String),
        typeof(WatermarkTextBox), new PropertyMetadata(null));

    public String WatermarkText
    {
        get { return watermarkTextBox.Text; }
        set { OnPropertyChanged("WatermarkText"); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string name)
    {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
     }
     private void watermarkTextBox_TextChanged(object sender, TextChangedEventArgs e)
     {
           WatermarkText = this.watermarkTextBox.Text;
     }

}

[MainWindow].xaml: [MainWindow] .xaml:

      <TextBlock Text="{Binding ElementName=usernameArea Path=WatermarkText}" .../> 

Adding a dependency property essentially allows you to expose values in your user control for modification in XAML (as well as bindings, in general). 本质上,添加依赖项属性使您可以在用户控件中公开要在XAML中进行修改的值(以及通常的绑定)。


You might also want to change the Foreground (text color) property of the TextBlock to something darker than white, because, by default, the Background is white. 您可能还需要将TextBlockForeground (文本颜色)属性更改为比白色更深的颜色,因为默认情况下, Background为白色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM