Popup中的WPF Popup就像一个菜单

时间:2015-06-05 18:58:44

标签: wpf popup

问题:当第一个弹出窗口出现时,第二个弹出窗口没有打开。它可能是透明边框?

以下代码在弹出窗口中包含一个弹出窗口

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:Converters="clr-namespace:WpfApplication1">

    <Window.Resources>
        <ResourceDictionary>
            <Converters:MultiBooleanOrConverter x:Key="multiBooleanOrConverter" />
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <Button x:Name="generatebtn" Content="Button" Height="25" Margin="10" Width="60"/>
        <!--Firs PopUp-->
        <Popup x:Name="popup" PlacementTarget="{Binding ElementName=generatebtn}"
            Placement="Top" Width="146" AllowsTransparency="True">
            <Popup.IsOpen>
                <MultiBinding Converter="{StaticResource multiBooleanOrConverter}">
                    <Binding ElementName="generatebtn" Path="IsMouseOver"  Mode="OneWay"/>
                    <Binding ElementName="popup" Path="IsMouseOver"  Mode="OneWay"/>
                </MultiBinding>
            </Popup.IsOpen>
            <StackPanel Orientation="Vertical" x:Name="ff">
                <!--First Item-->
                <StackPanel>
                    <Button x:Name="Button1">
                        <StackPanel>
                            <TextBlock Text="1 Item" Foreground="White"></TextBlock>
                            <!--Second PopUp-->
                            <Popup x:Name="secondPopup" IsOpen="{Binding ElementName=Button1, Path=IsMouseOver, Mode=OneWay}" PlacementTarget="{Binding ElementName=Button1}" HorizontalOffset="{Binding ElementName=Button1, Path=ActualWidth}" VerticalOffset="{Binding ElementName=Button1, Path=ActualHeight}"  Placement="Top" AllowsTransparency="True">
                                <Border Background="Transparent" Padding="5 0 0 0">
                                    <Border>
                                        <StackPanel Orientation="Vertical">
                                            <StackPanel>
                                                <Button x:Name="SubButton1" Background="Black">
                                                    <StackPanel>
                                                        <TextBlock Text="1 SubItem" Foreground="White"></TextBlock>
                                                    </StackPanel>
                                                </Button>
                                            </StackPanel>
                                            <StackPanel>
                                                <Button x:Name="SubButton2">
                                                    <StackPanel>
                                                        <TextBlock Text="2 SubItem" Foreground="White"></TextBlock>
                                                    </StackPanel>
                                                </Button>
                                            </StackPanel>
                                        </StackPanel>
                                    </Border>
                                </Border>
                            </Popup>
                        </StackPanel>
                    </Button>
                </StackPanel>
                <!--Second Item-->
                <StackPanel>
                    <Button x:Name="Button2">
                        <StackPanel>
                            <TextBlock Text="2 Item" Foreground="White"></TextBlock>
                        </StackPanel>
                    </Button>
                </StackPanel>
            </StackPanel>
        </Popup>
    </Grid>
</Window>

这是MultiBooleanOrConverter for MultiBinding

public class MultiBooleanOrConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var returnedValue = false;

        foreach (var value in values)
        {
            var boolValue = (bool)value;
            returnedValue = returnedValue || boolValue;
        }
        return returnedValue;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

1 个答案:

答案 0 :(得分:0)

为第二个弹出窗口设置Panel.ZIndex属性(也为第一个弹出窗口设置更清晰的代码)。例如:

 <Popup x:Name="popup" PlacementTarget="{Binding ElementName=generatebtn}"
        Placement="Top" Width="146" AllowsTransparency="True" Panel.ZIndex="10">
.
.
.
                        <!--Second PopUp-->
                        <Popup x:Name="secondPopup" IsOpen="{Binding ElementName=Button1, Path=IsMouseOver, Mode=OneWay}" ... Panel.ZIndex="20">
.
.
.                            
                        </Popup>
.
.
.
    </Popup>