views:

46

answers:

3

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?

A: 

Have you tried a StackPanel instead?

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

Is your IsEnabledProperty a dependency property?

Holstebroe
Yeah. Stack panel's the right way to go. I'm not sure where I saw TextBlock used this way originally!? Time to break a bad habit.
Scott P
+4  A: 

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.

John Bowen
A: 

Are you sure that your raises the PropertyChanged event for your property IsEnableProperty when you update it in the viewModel ?

Gerrrard