1

From my understanding of attached properties, I believe that I can set a property value that will apply to all of the children of a container that match the type. For instance, if I have a number of TextBoxes in a StackPanel, then I can disable them all by setting the TextBox.IsEnabled property to false in the StackPanel's declaration:

<StackPanel TextBox.IsEnabled="False" Orientation="Horizontal">
...
</StackPanel>

I tried this in Visual Studio, and the Xaml designer greyed-out the TextBoxes in the StackPanel exactly as expected, but when I tried to compile, I ran into the error:

The attachable property 'IsEnabled' was not found in type 'TextBox'

Have I misunderstood attached properties? Do they only go from the ancestor to the child? If so, is there a way to do what I am trying, ie, to set all child TextBoxes IsEnabled property to false?

Thanks for any pointers

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
mcalex
  • 6,628
  • 5
  • 50
  • 80
  • http://stackoverflow.com/questions/10051226/how-to-set-isreadonly-isenabled-on-entire-container-like-panel-or-groupbox-usi try to look at this – acrilige Jan 29 '13 at 05:46

1 Answers1

1

Yes, attached properties allow to set a property value on the parent and the children inherit that value. On the other hand TextBox.IsEnabled is not an attached property and so you cannot do what you want.

Perhaps it is possible to get what you want with some custom panels and/or custom attached properties programming.

However you could also get the same result using a Style where you can also bind the IsEnabled property to your custom logic if you need.

<StackPanel Width="200" Height="50" >
     <StackPanel.Resources>
         <Style TargetType="TextBox">
             <Setter Property="IsEnabled" Value="False" />
         </Style>
     </StackPanel.Resources>
        <TextBox Text="one" />
        <TextBox Text="two" />
</StackPanel>
Klaus78
  • 11,648
  • 5
  • 32
  • 28
  • I'm only dealing with a few textboxes and they are static (ie always RO), so I was just looking to find a shortcut to save me typing IsEnabled="False" a handful of times. I've obviously not quite got Attached properties from my readings, I thought they would bubble up as well as tunnel down. Oh well. I've now decided to just set IsEnabled in each of the TextBoxes. thanks for looking in. You've said I can't do it, so I'm calling it the answer. cheers. – mcalex Jan 29 '13 at 09:08
  • Tunneling and bubbling concepts are related to routed events. In case of (attached) properties one speaks about value inheritance. – Klaus78 Jan 29 '13 at 09:36