How to set IsOpen property in wpf popup window to True?
I have this wpf style:
<Style x:Key="RequiredControlTemplate">
<Style.Setters>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<Popup IsOpen="{Binding ElementName=Adorner, Path=IsMouseOver}">
<TextBlock Text="{Binding ElementName=Adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
</Popup>
开发者_如何学运维 <Border Grid.Column="1" BorderBrush="Red" BorderThickness="1" HorizontalAlignment="Stretch">
<AdornedElementPlaceholder Name="Adorner"/>
</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
I want that if mouse is over Adorner then Popup is open, but IsOpen="{Binding ElementName=Adorner, Path=IsMouseOver}
not work...
I made few changes in your style. added couple of story boards to play with pop up open... and trigger storyboard on adorner element
<Style x:Key="RequiredControlTemplate">
<Style.Setters>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<StackPanel.Resources>
<Storyboard x:Key="OnMouseEnter1">
<BooleanAimationUsingKeyFrames Storyboard.TargetProperty="(Popup.IsOpen)" Storyboard.TargetName="popup">
<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="OnMouseLeave1">
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(Popup.IsOpen)" Storyboard.TargetName="popup">
<DiscreteBooleanKeyFrame KeyTime="0" Value="True"/>
<DiscreteBooleanKeyFrame KeyTime="0:0:0.1" Value="False"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</StackPanel.Resources>
<Popup Name="popup">
<TextBlock Text="{Binding ElementName=Adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
</Popup>
<Border Grid.Column="1" BorderBrush="Red" BorderThickness="1" HorizontalAlignment="Stretch">
<AdornedElementPlaceholder Name="Adorner">
<AdornedElementPlaceholder.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseLeave" SourceName="Adorner">
<BeginStoryboard x:Name="OnMouseLeave1_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave1}"/>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseEnter" SourceName="Adorner">
<BeginStoryboard Storyboard="{StaticResource OnMouseEnter1}"/>
</EventTrigger>
</AdornedElementPlaceholder.Triggers>
</AdornedElementPlaceholder>
</Border>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
Add Mode=OneWay
to binding. IsOpen is TwoWay
by default and read-only properties doesn't allow TwoWay
binding.
If you don't follow by MVVM pattern, you can specify x:code
in your xaml, where you can handle MouseOver Event, and use EventSetter
to attach it.
精彩评论