我有很多文本框分布在三个单独的 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 的正确路径。
但最终以编程方式创建的文本框不会显示原始文本框的内容。它只是保持空白。
一只甜甜圈
相关分类