2012-05-05 82 views
4

会怎么做一个一个iPad一样覆盖窗口这样 在WPF XAML与 enter image description here像iPad的覆盖弹出窗口

我想过一个切换按钮,或扩展控制。 如果面板是可缩放的,那将会很不错。 我有最大的问题与中心覆盖有不同的大小,然后按钮本身。

任何帮助,链接或资源将是伟大的。

+0

实际WPF [Popup](http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.aspx)?我会创建一个名为PopupButton的类来处理所有这些。但是使用Popup有时候会非常棘手。特别是当你想通过点击外部关闭他们,或者当你点击一个按钮时关闭他们。学习如何使用它们的最好方法是查看WPF工具栏类中的溢出逻辑。 – dowhilefor

回答

3

dowhilefor是正确的,弹出类是要走的路 - 我做了一个小样本项目使用弹出与自定义控件作为孩子。最重要的是Popup的PlacementTarget和Placement字段,因为它们让你设置弹出窗口的位置。希望这可以帮助!

Popup example

自定义控制:

<UserControl x:Class="SilverfighterTest.PopupControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<UserControl.Resources> 
    <Style TargetType="Button"> 
     <Setter Property="Margin" Value="4"/> 
     <Setter Property="Padding" Value="7"/> 
    </Style> 
</UserControl.Resources> 
<Grid Background="Gray"> 
    <StackPanel> 
     <Button>Clone</Button> 
     <Button>Log Call</Button> 
     <Button> Visit Report</Button> 
     <Button> Delete</Button> 
     <Button>Cancel</Button> 
    </StackPanel> 

</Grid> 
</UserControl> 

窗口使用弹出:背后

<Window x:Class="SilverfighterTest.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SilverfighterTest" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <Rectangle VerticalAlignment="Top" Name="rect" MouseLeftButtonDown="rect_MouseLeftButtonDown" HorizontalAlignment="Right" Height="50" Width="50" Fill="Red"> 

    </Rectangle> 

    <Popup PopupAnimation="Slide" Placement="Bottom" PlacementTarget="{Binding ElementName=rect}" Name="thePopup" > 
     <local:PopupControl/> 
    </Popup> 
</Grid> 

代码窗口:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

    } 

    private void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     thePopup.IsOpen = !thePopup.IsOpen; 
    } 
} 
+0

感谢您的示例...是否有机会让矩形下方的Popup为中心?或右对齐像在我的截图? – silverfighter

+0

很高兴帮助:)您可以设置弹出控件的Horizo​​ntalOffset属性以根据您的喜好调整对齐。 thePopup.Horizo​​ntalOffset = -1 * rect.ActualWidth;应该为我的示例对齐弹出窗口。 – codechinchilla

+0

但这显然在codebehind ...我asume没有XAML的方式,然后写一个转换器或附加属性 – silverfighter