1

The code at the bottom makes the DockPanel Visibility=Collapsed whenever the TextBlock has an empty string value, which is what I want. However, I happened on this by accident and am wondering why setting the DockPanel.Visibility attached property in the TextBlock affects the DockPanel.

I know that DockPanel.Dock is an attached property you can directly set in an element e.g.

    <TextBlock DockPanel.Dock="Top"/>

..but you cannot set

    <TextBlock DockPanel.Visibility="Collapsed"/>

..in the same way.

So how does DockPanel know to query child elements for DockPanel.Visibility, or do parent elements always query children for all attached properties and use these whenever the value is not set locally? I was under the impression only certain attached properties were used in this way (e.g. DockPanel.Dock).

Also, what other ways are there to acheive the same result (e..g using triggers set within a DockPanel style - problem there seems to be errorTextBlock name is not in scope)

    <DockPanel x:Key="errorDisplay" LastChildFill="False">
            <Border Background="Red" DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1">
                <TextBlock Padding="4" x:Name="errorTextBlock">
                        <TextBlock.Style>
                            <Style>
                                <Style.Triggers>
                                    <Trigger Property="TextBlock.Text" Value="">
                                    <Setter Property="DockPanel.Visibility" Value="Collapsed"/>
                                    </Trigger>
                                </Style.Triggers>
                             </Style>
                        </TextBlock.Style>
                </TextBlock>
            </Border>            
    </DockPanel>
sturdytree
  • 849
  • 3
  • 12
  • 26
  • If you declare the code you posted not as a resource, but rather just put it in the window/control visual tree, it should work correctly and allow you to refer to `errorTextBlock` in code behind. To answer you questions about attached properties, take a look here: http://stackoverflow.com/questions/1173494/how-exactly-do-attached-properties-work-in-wpf and here: http://msdn.microsoft.com/en-us/library/ms749011.aspx. – Lukasz M May 19 '12 at 12:12
  • @Lucas, thanks for that, and those links do go some way to clearing up my questions - I'm still not sure whether all library (i.e. not user defined) classes defining attached properties ALWAYS examine child elements for values set. Regarding inserting the code directly in the containing element, unfortunately this Dockpanel is needed in many places, hence defining it as a resource. Maybe someone can explain how to refer to errorTextBlock, as it doesn't seem sensible to not allow this here but to allow it in direct placement – sturdytree May 19 '12 at 13:37

0 Answers0