juFo
juFo

Reputation: 18567

WPF binding not applied - why does it happen

I wanted to update a stringformat of my binding via a datatrigger. So I thought of just doing this:

<TextBlock Text="{Binding Foo.Name}" Margin="3">
                  <TextBlock.Style>
                    <Style TargetType="TextBlock">
                      <Setter Property="Text" Value="{Binding Path=., StringFormat='Start {0}'}" />
                      <Style.Triggers>
                        <DataTrigger Binding="{Binding Foo.IsEnabled}" Value="True">
                        <Setter Property="Text" Value="{Binding Path=., StringFormat='Stop {0}'}" />
                      </DataTrigger>
                      </Style.Triggers>
                    </Style>
                  </TextBlock.Style>
                </TextBlock>

now this just shows the value of Foo.Name and not with the string format applied('start'/'stop' string).

I've modified the code, to get it working. This is the working code:

        <TextBlock Margin="3">
          <TextBlock.Style>
            <Style TargetType="TextBlock">
              <Setter Property="Text" Value="{Binding Foo.Name, StringFormat='Start {0}'}" />
              <Style.Triggers>
                <DataTrigger Binding="{Binding Foo.IsEnabled}" Value="True">
                  <Setter Property="Text" Value="{Binding Foo.Name, StringFormat='Stop {0}'}" />
                </DataTrigger>
              </Style.Triggers>
            </Style>
          </TextBlock.Style>
        </TextBlock>

Now i'm looking for the reason why the first piece of code doesn't work and why the second piece of code works. I've seen similar behavior in DataTemplates where I could not set or alter a property value via a Setter in a DataTrigger because the property was already set directly via the property on the object itself. Only when I removed the property from the object itself and set it as a style it worked.

Or is this just a limitation in WPF?

Upvotes: 1

Views: 270

Answers (1)

Derrick Moeller
Derrick Moeller

Reputation: 4950

I wouldn't call it a limitation, it's documented. Dependency properties have a value precedence. https://msdn.microsoft.com/en-us/library/ms743230(v=vs.110).aspx

Upvotes: 1

Related Questions