I have a Dock Panel and I want to set its height depending on a Property value.
The following compiles, but does not execute :
<DockPanel Grid.Row="1"
Visibility="{Binding Path=IsValid}" Margin="8,4">
<DockPanel.Triggers>
<Trigger Property="FrameworkElement.Visibility" Value="Visible">
<Setter Property="FrameworkElement开发者_如何学编程.Height" Value="150"/>
</Trigger>
<Trigger Property="FrameworkElement.Visibility" Value="Hidden">
<Setter Property="FrameworkElement.Height" Value="0"/>
</Trigger>
</DockPanel.Triggers>
<ListBox Height="150"/>
</DockPanel>
What am I doing wrong? Any help, greatly appreciated.
Thanks
Joe
From MSDN:
Note that the collection of triggers established on an element only supports EventTrigger, not property triggers (Trigger). If you require property triggers, you must place these within a style or template and then assign that style or template to the element either directly through the Style property, or indirectly through an implicit style reference.
So, for this to work you need a Style or a Template. I don't think you wish to change the way your DockPanel looks, so Style it is:
<DockPanel Grid.Row="1" Visibility="{Binding Path=IsValid}" Margin="8,4">
<DockPanel.Style>
<Style>
<Style.Triggers>
<Trigger Property="FrameworkElement.Visibility" Value="Visible">
<Setter Property="FrameworkElement.Height" Value="150"/>
</Trigger>
<Trigger Property="FrameworkElement.Visibility" Value="Hidden">
<Setter Property="FrameworkElement.Height" Value="0"/>
</Trigger>
</Style.Triggers>
</Style>
</DockPanel.Style>
<ListBox Height="150"/>
</DockPanel>
精彩评论