如何在wpf弹出窗口中设置IsOpen属性为True



我有一个wpf样式:

<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>

我想如果鼠标是在Adorner然后弹出是打开的,但IsOpen="{Binding ElementName=Adorner, Path=IsMouseOver}不工作…

我对你的风格做了一些改动。增加了一对夫妇的故事板玩弹出打开…并在装饰元素

上触发storyboard
<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>

Mode=OneWay加入绑定。IsOpen默认为TwoWay,只读属性不允许TwoWay绑定

如果您不遵循MVVM模式,您可以在xaml中指定x:code,在那里您可以处理MouseOver事件,并使用EventSetter来附加它

相关内容

  • 没有找到相关文章

最新更新