Question

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

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top