Search code examples
wpfdata-bindingtextblockisenabled

What's wrong with TextBlock IsEnabled binding?


I've got a simple text block with a label and text box as content. I would like to bind the IsEnabled property of the text block to a property on my view model. For whatever reason the label and text box stay disabled even though the IsEnabled property is changing properly on the view model.

Anyone know what's going on here?

This doesn't work:

<TextBlock IsEnabled="{Binding Path=IsEnabledProperty}">
    <Label Content="Test"/>
    <TextBox Text="blah"/>
</TextBlock>

This works just fine:

<TextBlock>
    <Label IsEnabled="{Binding Path=IsEnabledProperty}" Content="Test"/>
    <TextBox IsEnabled="{Binding Path=IsEnabledProperty}" Text="blah"/>
</TextBlock>

Is it just a bad idea to use TextBlock like this?


Solution

  • Yes, it's a bad idea. When you place non-string objects in the Text property it is used as content elements, like in a FlowDocument, and therefore isn't interactive like normal FrameworkElements.