如何使用buttonclick将UserControls文本框的值传递到不同的Usercontrols文本框?

时间:2018-01-31 16:34:32

标签: c# wpf data-binding

我的问题是,当用户单击FirstUserControl上的Button时,我想从UserControl(FirstUsercontrol)将Textbox值传递到不同的UserControls文本框(SecondUsercontrol)。 我在互联网上搜索了很多,发现了link

然而,我的问题仍然存在,我真的不知道如何解决这个问题。 如果我按照链接上提到的方式进行操作,我的SecondUsercontrol将从FirstUserControl获取所有内容,我只想将Textboxvalues传递给UserControl。

First UserControl.xaml.cs:

public partial class FirstUserControl: UserControl, INotifyPropertyChanged
{
    public FirstUserControl()
    {
        //this.DataContext = this;
        InitializeComponent();

    }

    public string ThemaString
    {
        get { return (string)GetValue(ThemaStringProperty); }
        set { SetValue(ThemaStringProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ThemaString.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ThemaStringProperty =
        DependencyProperty.Register("ThemaString", typeof(string), typeof(sseAnzeigen), new FrameworkPropertyMetadata(null) { BindsTwoWayByDefault = true });

FirstUserControls.xaml:

        <Label Content="Thema:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="8" VerticalAlignment="Center"/>
         <TextBox x:Name="ThemaText" Grid.Column="1" 
          HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="8" 
          Text="{Binding ThemaString, RelativeSource={RelativeSource 
          AncestorType={x:Type local:FirstUserControl}}/>

           <Button Content="Entwurfsansicht" Grid.Row="3"
             Click="Button_Click" /> 

SecondUsercontrol.xaml.cs:

 <UserControl.DataContext>
    <local:FirstUserControl/>
</UserControl.DataContext>
  <TextBlock Text="Thema:" MinWidth="{Binding ActualWidth, 
   ElementName=textBlock, Mode=OneWay}" />
  <TextBox x:Name="ThemaText" Text="{Binding ThemaString}">

我试图将代码剪切成我的问题。希望这足以理解我的意思:)。 谢谢!!

0 个答案:

没有答案