I have a lot of textboxes distributed over three individual TabItems and I want to give the User the option to choose favorite texboxes and display them on one tabItem called Favorites for convenience.
The visual part works fine, but I can't seem to get the bindings working, so the new Textbox stays empty.
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# of the binding
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
[...]
*) I tried the cloneMethod from Binding does not have a Clone method, whats an effective way to copy it but that didnt work aswell.
During debug oldBind.Path
shows the correct path to Article.Name.
But in the end the programmatically created textbox won't show the content of the original Textbox. It simply stays empty.