我有很多文本框分布在三个单独的TabItems上,我想让用户选择喜欢的texbox并将它们显示在一个名为Favorites的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 does not have a Clone method, whats an effective way to copy it的cloneMethod 但那并没有起作用。
在调试期间,oldBind.Path
显示了Article.Name的正确路径。
但最终以编程方式创建的文本框不会显示原始文本框的内容。它只是保持空白。
答案 0 :(得分:1)
最后我明白了!
我必须将绑定源设置为原始TextBox并将Binding设置为Text。 这并不是100%的问题,因为这可能不是关于@AlvinfromDiaspar评论的最有效的解决方案 Keep two textboxes synchronized in WPF 但无论如何,它适用于我的目的。
我改变了什么:
Binding binding = new Binding("Text");
binding.Source = (DependencyObject)this.FindName(BindingName);
绑定的整个片段:
String BindingName = inName.Substring(3);
BindingBase BB = BindingOperations.GetBindingBase(
(DependencyObject)this.FindName(BindingName),
TextBox.TextProperty);
var oldBind = BB as Binding;
if (BB != null)
{
//the magic goes here
Binding binding = new Binding("Text");
binding.Source = (DependencyObject)this.FindName(BindingName);
binding.Mode = BindingMode.OneWay;
binding.IsAsync = false;
binding.UpdateSourceTrigger = UpdateSourceTrigger.Default;
binding.TargetNullValue = "null";
BindingOperations.SetBinding(tb, TextBox.TextProperty, binding);
}
答案 1 :(得分:0)
不是尝试复制绑定,只需复制控件(使用其数据绑定,但不复制其名称),然后(绑定到其可见性属性),然后根据用户选择显示或隐藏文本框控件。
<TextBox Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"
Style="{StaticResource Heading2}"
Text="{Binding Article.Name, UpdateSourceTrigger=PropertyChanged}"
Visibility = "{Binding AmIVisible_1}" />
<TextBox Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"
Style="{StaticResource Heading3}"
Text="{Binding Article.Name, UpdateSourceTrigger=PropertyChanged}"
Visiblity="{Binding AmIVisible_2}" />