be_mi
be_mi

Reputation: 569

WPF influencing the order of databinding?

I have created an AttachedProperty that has a PropertyChangedCallback function, which does some formatting to a TextBlock. But to perform the formatting, the Tag attribute is needed. The Tag itself is bound to the output of a Multiconverter.

But my problem is, that the FNamePropertChangedCallback is executed before the Tag is bound to the output of the Multibinding. Thus Tag still Null, when FNamePropertyChangedCallback is triggered.

Is there any way to influence the order in which the Attributes are bound? I need to bind Tag before binding FInlineProperty.

public static readonly DependencyProperty FInlinePropertyProperty =
    DependencyProperty.RegisterAttached("FInlineProperty", typeof(string), typeof(MainWindow),
      new PropertyMetadata(null, FNamePropertyChangedCallback));

<TextBlock local:MainWindow.FInlineProperty="{Binding Name}" TextWrapping="WrapWithOverflow">
  <TextBlock.Tag>
  <MultiBinding Converter="{StaticResource TupleConverter}">
      <Binding />
      <Binding ElementName="Window"/>
    </MultiBinding>
  </TextBlock.Tag>
</TextBlock>

Btw: Does someone know how to write local:MainWindow.FInlineProperty="{Binding Name}" in long form? I tried <TextBlock.local:MainWindow.FInlinse>...</TextBlock.local:MainWindow.FInlinse>, but the compiler complains about it.


Update 25.Feb.2017:

I did what you suggested and bound everything to my Attached Property.

<TextBlock TextWrapping="WrapWithOverflow" >
<local:MainWindow.FInlineProperty>
  <MultiBinding Converter="{StaticResource GroupConverter}">
    <Binding  />
    <Binding ElementName="Window" Path="MySetup" />
  </MultiBinding>    
</local:MainWindow.FInlineProperty>
</TextBlock>

The problem with this is, that it is working the first time when the ListBoxItem is created. But updating properties from MySetup does not re-trigger FNamePropertyChangedCallback.

  MySetup.ColorString = "green";  // this does not retrigger the callback
  MySetup = MySetup.Copy();       // this does retrigger the callback

So currently only assigning MySetup a new copy of itself (changing the reference) triggers callback function again.

That was the reason, why I bound the name property explicitly.

MySetup.ColorString would trigger, if I bind to it explicitly, but I need to bind to MySetup to have all data, so the question is, how to force the binding to execute again, when the bound object itself (reference) has not changed, but something inside did change?

Upvotes: 1

Views: 479

Answers (2)

mm8
mm8

Reputation: 169200

Try to set the Tag property before you set the attached property. This means that you should also set the attached property using element syntax:

<TextBlock TextWrapping="WrapWithOverflow">
    <TextBlock.Tag>
        <MultiBinding Converter="{StaticResource TupleConverter}">
            <Binding />
            <Binding ElementName="Window"/>
        </MultiBinding>
    </TextBlock.Tag>
    <local:MainWindow.FInlineProperty>
        <Binding Path="Name" />
    </local:MainWindow.FInlineProperty>
</TextBlock>

Upvotes: 0

AnjumSKhan
AnjumSKhan

Reputation: 9827

What you actually want is binding between your AttachedProperty and TextBlock.Tag property, so that AttachedProperty is set when Tag changes. But since you are also binding to Name, so I suggest to use MultiBinding for your AP using Name and Tag bindings, thus not depending upon Tag at all.

Upvotes: 1

Related Questions