【问题标题】:How to bind textboxes in different user controls如何在不同的用户控件中绑定文本框
【发布时间】:2013-07-29 07:22:16
【问题描述】:

我有一个主窗口,其中有一个 UserControl1 和 UserControl2,它们都包含一个 TextBox。

绑定这两个文本框的 Text 属性的最佳方法是什么。

MainWindow.xaml

<Window x:Class="DataBindTest1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:DataBindTest1">
    <StackPanel>
        <Controls:UserControl1/>
        <Controls:UserControl2/>
    </StackPanel>
</Window>

UserControl1.xaml

<UserControl x:Class="DataBindTest1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <TextBox Name="uc1TextBox">ExampleText</TextBox>
    </Grid>
</UserControl>

UserControl2.xaml

<UserControl x:Class="DataBindTest1.UserControl2"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
            <TextBox Name="uc2TextBox" /> <!--I want this to be bound to the Text property of uc1TextBox (which is in UserControl1)-->
    </Grid>
</UserControl>

提前感谢您的帮助,

维杰

【问题讨论】:

  • 我把 UserControl2.xaml 改成了这个,但没有用:'schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft.com/winfx/2006/xaml" xmlns: Controls="clr-namespace:DataBindTest1"> '

标签: wpf data-binding user-controls


【解决方案1】:

您可以将两个 TextBox 中的 Text 属性绑定到同一 ViewModel 对象的属性,该对象设置为 MainWindowDataContext 并继承到 UserControls:

<UserControl x:Class="DataBindTest1.UserControl1" ...>
    <Grid>
        <TextBox Text="{Binding SomeText}"/>
    </Grid>
</UserControl>

<UserControl x:Class="DataBindTest1.UserControl2" ...>
    <Grid>
        <TextBox Text="{Binding SomeText}"/>
    </Grid>
</UserControl>

<Window x:Class="DataBindTest1.MainWindow" ...>
    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>
    <StackPanel>
        <Controls:UserControl1/>
        <Controls:UserControl2/>
    </StackPanel>
</Window>

两个 UserControl 都绑定到的具有 Text 属性的 ViewModel 类:

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private string someText;
    public string SomeText
    {
        get { return someText; }
        set
        {
            someText= value;
            OnPropertyChanged("SomeText");
        }
    }
}

【讨论】:

  • 嗨克莱门斯。这适用于我想做的事情。并帮助我加深了对 WPF 数据绑定的理解。谢谢你。 (抱歉,我不能对您的回答投赞成票,因为我今天才加入 Stack Overflow,并且没有最低声誉级别来这样做)。
  • 不客气。您可以通过检查左侧的接受标记来接受答案。请参阅here 它是如何工作的。
  • 哦,是的,你就是这样做的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 2014-01-26
  • 2021-08-10
相关资源
最近更新 更多