单击按钮位置打开弹出窗口

时间:2015-06-15 06:48:54

标签: c# wpf

我正在尝试创建一个类似于Windows 8任务栏中的窗口预览选项的弹出窗口。基本上,当我单击或悬停在按钮上时,它会打开一个弹出窗口,其中包含按钮上方的基本信息。这是一个例子。 Windows 8 taskbar

现在,我可以使用下面的代码在页面的最左侧或最右侧打开弹出窗口。

<Frame x:Name="PopUpFrame" HorizontalAlignment="Left" Visibility="Hidden" Height="400" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Bottom" Width="400" Source="/BasicApp;component/StandingOrderPopUp.xaml" NavigationUIVisibility="Hidden"/>

按钮栏中有3个不同的按钮,因此无法设置固定边距。我试图在代码中设置相同但我无法获得绝对位置并将其转换为margin属性。我不确定,是否有更好的解决方案。

编辑:

尝试使用弹出窗口,但在点击按钮时不会打开。

        <Popup x:Name="PopUpFrame" Placement="Top" PlacementTarget="{Binding ElementName=StandingOrderButton}" Width="400" Height="400">
        <DockPanel Background="#770081a7">
            <Canvas DockPanel.Dock="Bottom" x:Name="PopupButtonBar" Height="50" VerticalAlignment="Bottom">
                <Button Height="30" Width="125" HorizontalAlignment="Right" VerticalAlignment="Center" Canvas.Top="10" Content="CLOSE" Foreground="White" Background="#ff0081a7" BorderBrush="White" FontFamily="Trebuchet MS" Canvas.Left="10" />
            </Canvas>
            <Label Margin="5,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DockPanel.Dock="Top" Content="STANDING ORDERS" Foreground="#ffffffff"></Label>
            <Grid DockPanel.Dock="Top">
                <Border BorderThickness="2" BorderBrush="#FFff7a00"></Border>
                <RichTextBox Margin="2" Foreground="#FF0081a7" FontFamily="Trebuchet MS" IsEnabled="False"/>
            </Grid>
        </DockPanel>
    </Popup>

这是事件处理程序。

    Private Sub StandingOrder_Click(sender As Object, e As RoutedEventArgs)
    PopUpFrame.Visibility = Windows.Visibility.Visible
End Sub

编辑:

没关系。我是一个白痴。我没有设置IsOpen属性,而是设置了可见性。 :(

它完美无缺,但我必须将整个设计从单独的页面复制到此页面。总比没有好。现在唯一的问题是,如果我点击其他内容,我将不得不编写代码以确保弹出窗口已关闭。

3 个答案:

答案 0 :(得分:3)

您可以使用Placement控件,再加上<Popup Placement="Top" PlacementTarget="{Binding ElementName=yourButton}" ... /> 属性,根据按钮的当前位置显示弹出窗口。

Popup

UserControl内,您可以放置​​template<typename T> class Test { public: virtual T a() = 0; virtual void b(T t) = 0; }; 或任何将充当弹出窗口内容的内容元素。

有关弹出窗口布局的详细信息,请参阅here;有关WPF弹出控件的教程,请参阅here

答案 1 :(得分:0)

考虑使用ContextMenu,并根据需要将其设置为内容。那就是你的解决方案。

答案 2 :(得分:0)

这是我定义我的一个弹出窗口的方式:

<Popup x:Name="btnPowerPopup" Placement="Mouse" StaysOpen="False">
....
</Popup>

您可以通过点击按钮或其他内容使用后面的代码:

btnPowerPopup.IsOpen = true;

当工作完成时:

btnPowerPopup.IsOpen = false;

StaysOpen="False"让你在点击其他地方时关闭PopUp。