基于两个属性显示弹出窗口(IsFocused,IsVisible)

时间:2018-01-17 09:27:11

标签: c# wpf xaml popup datatrigger

我有弹出错误消息框,它会在文本框聚焦后打开。

打开对话框窗口时的问题,如果单击关闭按钮(关闭时我隐藏窗口)错误消息保持可见。我试图添加触发器以检查可见性,但它没有工作。任何想法?

XAML:

<ControlTemplate x:Key="errorToolTipTemplate">
    <Grid>
        <Border BorderBrush="#EF5B7C" BorderThickness="1" Background="#EF5B7C" Opacity="0.4" IsHitTestVisible="False" x:Name="errorBorder" />
        <AdornedElementPlaceholder x:Name="placeholder" />
        <Popup x:Name="popup" AllowsTransparency="True"
               PopupAnimation="Fade"
               Placement="Right"
               VerticalOffset="-3"
               PlacementTarget="{Binding ElementName=errorBorder}"
               IsOpen="{Binding ElementName=placeholder, Path=AdornedElement.IsFocused, Mode=OneWay}">
            <Popup.Style>
                <Style TargetType="{x:Type Popup}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=errorBorder, Path=IsVisible, Mode=OneWay}" Value="False">
                            <Setter Property="IsOpen" Value="False" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Popup.Style>
            <DockPanel>
                <Polygon VerticalAlignment="Center"
                         Points="0,5 5,0 5,10"
                         Fill="#EF5B7C"
                         Stretch="Fill" />
                <Border Background="White" BorderBrush="#EF5B7C" BorderThickness="1" CornerRadius="4" Padding="2">
                    <DockPanel>
                        <Image Height="20" Width="20" VerticalAlignment="Center" Source="Images/C04_32.png" />
                        <TextBlock HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   Foreground="{StaticResource errorBrush}"
                                   FontWeight="Bold" Margin="2,0,0,0"
                                   Text="{Binding ElementName=placeholder, Path=AdornedElement.ToolTip, Mode=OneWay}" />
                    </DockPanel>
                </Border>
            </DockPanel>
        </Popup>
    </Grid>
</ControlTemplate>

<!--Textbox style-->
<Style TargetType="{x:Type TextBox}">
    <Setter Property="Validation.ErrorTemplate" Value="{DynamicResource errorToolTipTemplate}" />
</Style>

1 个答案:

答案 0 :(得分:0)

通过为triggers添加ControlTemplate来解决此问题。 我添加了SourceTarget触发器作为adornerTargetName作为弹出窗口,并根据其他控件可见性更改了IsOpen属性。

<ControlTemplate x:Key="errorToolTipTemplate">
    <Grid>
        <Border BorderBrush="#EF5B7C" BorderThickness="1" Background="#EF5B7C" Opacity="0.4" IsHitTestVisible="False" x:Name="errorBorder" />
        <AdornedElementPlaceholder x:Name="placeholder" />
        <Popup x:Name="popup" AllowsTransparency="True"
               PopupAnimation="Fade"
               Placement="Right"
               VerticalOffset="-3"
               PlacementTarget="{Binding ElementName=errorBorder}"
               IsOpen="{Binding ElementName=placeholder, Path=AdornedElement.IsFocused, Mode=OneWay}">
            <DockPanel>
                <Polygon VerticalAlignment="Center"
                         Points="0,5 5,0 5,10"
                         Fill="#EF5B7C"
                         Stretch="Fill" />
                <Border Background="White" BorderBrush="#EF5B7C" BorderThickness="1" CornerRadius="4" Padding="2">
                    <DockPanel>
                        <Image Height="20" Width="20" VerticalAlignment="Center" Source="Images/C04_32.png" />
                        <TextBlock HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   Foreground="{StaticResource errorBrush}"
                                   FontWeight="Bold" Margin="2,0,0,0"
                                   Text="{Binding ElementName=placeholder, Path=AdornedElement.ToolTip, Mode=OneWay}" />
                    </DockPanel>
                </Border>
            </DockPanel>
        </Popup>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger SourceName="placeholder" Property="IsVisible" Value="False">
            <Setter TargetName="popup" Property="IsOpen" Value="False" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>