复制一个文本框,包括它的绑定

我有很多文本框分布在三个单独的 TabItem 上,为了方便起见,我想让用户选择最喜欢的文本框并将它们显示在一个名为“收藏夹”的 tabItem 上。


视觉部分工作正常,但我似乎无法让绑定工作,所以新的文本框保持为空。


XAML:


<StackPanel Grid.Row="3" Grid.Column="0" x:Name="SP_TB_DP_ArticleName"     x:Uid="ArticleName" Style="{StaticResource FavButton}"  MouseDown="FavButton_Click" >

   <Label  Content="ArticleName"  Style="{StaticResource Heading2}" />

    <Border Background="Transparent">

      <StackPanel Orientation="Horizontal">

        <Path Uid="FavAdd" Style="{StaticResource Ico-AddFav}" Fill="Black"/>

        <Path Uid="FavRem" Style="{StaticResource Ico-RemFav}" Fill="Black" Visibility="Collapsed" />

      </StackPanel>

     </Border>

    </StackPanel>

<TextBox  x:Name="TB_DP_ArticleName"  Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Style="{StaticResource Heading3}" Text="{Binding Article.Name, UpdateSourceTrigger=PropertyChanged}" />

C#的绑定


private void FavButton_Click(object sender, MouseButtonEventArgs e)

{

[... creating textbox and label]


//BindingName is the name of the original textbox ie: TB_DP_ArticleName

DependencyObject OriginalTB = (DependencyObject) this.FindName(BindingName);

BindingBase BB = BindingOperations.GetBindingBase(OriginalTB, TextBox.TextProperty);

var oldBind = BB as Binding;


//Clone Method from other question Link see below *

//BindingBase copyBind= CloneBinding(BB, OriginalTB);



 if (OriginalTB != null)

 {

  Binding binding = new Binding();

  binding.Source = this;

  binding.Path = oldBind.Path;

  binding.Mode = BindingMode.OneWay;

  binding.IsAsync = false;

  binding.UpdateSourceTrigger = UpdateSourceTrigger.Default;

  binding.TargetNullValue = "null";

  BindingOperations.SetBinding(tb, TextBox.TextProperty, binding);

  //BindingOperations.SetBinding(tb, TextBox.TextProperty, copyBind);

 }


//Code to add the Textbox to the Grid 

[...]

*) 我尝试了Binding 中的 cloneMethod没有 Clone 方法,这是一种复制它的有效方法, 但它也不起作用。


在调试期间oldBind.Path显示到 Article.Name 的正确路径。


但最终以编程方式创建的文本框不会显示原始文本框的内容。它只是保持空白。


MYYA
浏览 164回答 2
2回答

一只甜甜圈

最后我明白了!我必须将绑定源设置为原始文本框并将绑定设置为文本。这并不能 100% 回答问题,因为这可能不是关于在 WPF中保持两个文本框同步中的@AlvinfromDiaspar 评论的最有效解决方案,&nbsp;但无论如何,它对我的目的来说足够好。我改变了什么:Binding binding = new Binding("Text");&nbsp;binding.Source = (DependencyObject)this.FindName(BindingName);绑定的整个片段:String BindingName = inName.Substring(3);BindingBase BB = BindingOperations.GetBindingBase(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(DependencyObject)this.FindName(BindingName),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox.TextProperty);var oldBind = BB as Binding;if (BB != null){&nbsp; //the magic goes here&nbsp; Binding binding = new Binding("Text");&nbsp; binding.Source = (DependencyObject)this.FindName(BindingName);&nbsp; binding.Mode = BindingMode.OneWay;&nbsp; binding.IsAsync = false;&nbsp; binding.UpdateSourceTrigger = UpdateSourceTrigger.Default;&nbsp; binding.TargetNullValue = "null";&nbsp; BindingOperations.SetBinding(tb, TextBox.TextProperty, binding);&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP