1

I've a CustomControl which has an Items property. An internal TabControl is bound to it like this:

<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <TabControl Name="PART_TabControl"
                                >
                    </TabControl>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

OnApplyTemplate:

public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        TabControl tabControl = Template.FindName("PART_TabControl", this) as TabControl;
        foreach (var item in Items)
        {
            Dispatcher.BeginInvoke(new Action(() => tabControl.Items.Add(item)), System.Windows.Threading.DispatcherPriority.ContextIdle);
        }
    }    

To demonstrate the problem I've added "Dispatcher.BeginInvoke". The problem occurs when tabitems are added after a while. The usage below results in a binding failure:

<ccl:CustomControl1 Name="cc1">
        <ccl:TabItemCollection>
            <TabItem Header="Tab1">
                <TextBox Name="txtData" />
            </TabItem>
            <TabItem Header="Tab2">
                <TextBlock Text="{Binding ElementName=txtData, Path=Text}" />
            </TabItem>
        </ccl:TabItemCollection>
    </ccl:CustomControl1>    

I noticed that after snooping the textblock, the problem is fixed. And from the source code I see this code does the job:

BindingExpression expression = BindingOperations.GetBindingExpression(dObj, property);

                if (expression != null && !expression.HasError && expression.Status != BindingStatus.Active)
                {
                    dObj.ClearValue(property);
                    BindingOperations.SetBinding(dObj, property, expression.ParentBindingBase);
                }

As I've many controls in tab items this kind of "fix binding" code causes a lot of performance issues.

Is there any workaround for that?

Hamit
  • 623
  • 1
  • 8
  • 18
  • *An internal `TabControl` is bound to it like this*... you have nothing *bound* in your displayed `TabControl`. If you did, it would look more like ``. – Sheridan Jan 13 '15 at 09:02
  • If I do so, even "Fix Binding" doesn't work. I used this approach: http://www.codeproject.com/Articles/212233/Persist-the-Visual-Tree-when-switching-tabs-in-the – Hamit Jan 13 '15 at 09:17
  • 1. That was *your* claim... I was just pointing out that it was incorrect. 2. I have no idea why you're using that approach as data will be persisted as standard. – Sheridan Jan 13 '15 at 09:32

0 Answers0