wpf 实现childwindow

1.问题:最近手上有个项目,需要用到wpf, wpf没有silverlight的childwindow, 这样弹窗口不是很方便

2.解决办法:整合了一下网上意见,得到解决办法如下:  

public static  class ChildWindowBehaviorRegist
    {
        public static void Show(this FrameworkElement control)
        {
            Grid grid = GetRootGrid(control );
            RootAndMask ram = GetRootAndMask(control);

            if (grid != null && ram !=null )
            {
                DoubleAnimation opacityAnimation = new DoubleAnimation(0.5, new Duration(TimeSpan.FromSeconds(0.5)));

                Storyboard opacityBoard = new Storyboard();
                opacityBoard.Children.Add(opacityAnimation);

                Storyboard.SetTarget(opacityAnimation,  ram.MaskRectangle );
                Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("(Opacity)"));

                opacityBoard.Completed += delegate
                {
                    ScaleTransform scaleTransform = new ScaleTransform(0.0, 0.0, control.Width / 2.0, control.Height / 2.0);
                    control.RenderTransform = scaleTransform;

                    grid.Children.Add(control);

                    Storyboard scaleBoard = new Storyboard();

                    DoubleAnimation scaleXAnimation = new DoubleAnimation(1.0, TimeSpan.FromSeconds(0.5));

                    scaleBoard.Children.Add(scaleXAnimation);

                    Storyboard.SetTarget(scaleXAnimation, control);
                    Storyboard.SetTargetProperty(scaleXAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"));

                    DoubleAnimation scaleYAnimation = new DoubleAnimation(1.0, TimeSpan.FromSeconds(0.5));

                    scaleBoard.Children.Add(scaleYAnimation);

                    Storyboard.SetTarget(scaleYAnimation, control);
                    Storyboard.SetTargetProperty(scaleYAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleY)"));

                    scaleBoard.Begin();
                };

                opacityBoard.Begin();

                grid.Children.Add(ram.MaskRectangle);
            }
        }

        public static void Close(this FrameworkElement control)
        {
            Grid grid = GetRootGrid(control );
            RootAndMask ram = GetRootAndMask(control);
            if (grid != null)
            {
                ScaleTransform scaleTransform = new ScaleTransform(1.0, 1.0, control.Width / 2.0, control.Height / 2.0);
                control.RenderTransform = scaleTransform;

                Storyboard scaleBoard = new Storyboard();

                DoubleAnimation scaleXAnimation = new DoubleAnimation(0.0, TimeSpan.FromSeconds(0.5));

                scaleBoard.Children.Add(scaleXAnimation);

                Storyboard.SetTarget(scaleXAnimation, control);
                Storyboard.SetTargetProperty(scaleXAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"));

                DoubleAnimation scaleYAnimation = new DoubleAnimation(0.0, TimeSpan.FromSeconds(0.5));

                scaleBoard.Children.Add(scaleYAnimation);

                Storyboard.SetTarget(scaleYAnimation, control);
                Storyboard.SetTargetProperty(scaleYAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleY)"));

                scaleBoard.Completed += delegate
                {
                    DoubleAnimation opacityAnimation = new DoubleAnimation(0.5, 0.0, new Duration(TimeSpan.FromSeconds(0.5)));

                    Storyboard opacityBoard = new Storyboard();
                    opacityBoard.Children.Add(opacityAnimation);

                    Storyboard.SetTarget(opacityAnimation, ram.MaskRectangle);
                    Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("(Opacity)"));

                    opacityBoard.Completed += delegate
                    {
                        grid.Children.Remove(ram.MaskRectangle);
                        grid.Children.Remove(control);
                    };

                    opacityBoard.Begin();
                };

                scaleBoard.Begin();
            }
        }

        static RootAndMask GetRootAndMask(FrameworkElement control)
        {
            if (control == null || control.Tag == null || !(control.Tag is RootAndMask))
                return null;
            return  control.Tag as RootAndMask;
        }

        static Grid GetRootGrid(FrameworkElement control)
        {
            RootAndMask ram = GetRootAndMask(control);
            FrameworkElement root = ram.Root ;

            while (root is FrameworkElement && root.Parent != null)
            {
                FrameworkElement rootElement = root as FrameworkElement;

                if (rootElement.Parent is FrameworkElement)
                {
                    root = rootElement.Parent as FrameworkElement;
                }
            }
            ContentControl contentControl = root as ContentControl;
            return contentControl.Content as Grid;
        }
    }

    public class RootAndMask
    {
        public FrameworkElement Root { get; set; }
        public Rectangle MaskRectangle = new Rectangle { Fill = new SolidColorBrush(Colors.DarkGray), Opacity = 0.0 };
        public RootAndMask(FrameworkElement root)
        {
            this.Root = root;
        }
    }

3.调用:使用很简单,

编写一个user control

Login{.....},

 public MainWindow()
        {
            InitializeComponent();
            user = new User();
            client = new ChatClient(new InstanceContext(this ));
            Login login = new Login(user ,client);
            login.Tag = new RootAndMask(this);
            login.Show();
        }

4.利弊:

     4.1 利:范围广, 只要是继承自FrameworkElement 的,你都可以像childwindow一样使用

     4.2 弊: main page 需要 <mainpage><grid1><grid2></grid></grid><mainpage> 额外添加一个grid1,用grid2布局。tag被占用了。

转载于:https://www.cnblogs.com/mjgb/archive/2011/07/01/2095272.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPFWindows Presentation Foundation)是一种用于创建 Windows 应用程序的框架,它提供了丰富的图形和用户界面功能。实现像 XMind 这样的思维导图工具的过程如下: 1. 数据结构设计:首先需要设计思维导图的数据结构,包括节点(节点可能有不同的类型,如主题、子主题、注释等)、连接线等。可以使用树状结构或图结构来表示思维导图的组织关系。 2. 界面设计:使用 WPF 的图形和用户界面功能来设计思维导图的界面。可以使用画布(Canvas)来承载节点和连接线,通过鼠标事件来实现节点拖拽、连线等功能。可以为节点和连接线定义样式和模板,以美化界面和提供更多交互效果。 3. 数据绑定:将思维导图的数据模型与界面进行绑定,使得界面能够动态展示数据的变化。可以使用 WPF 的数据绑定机制,将节点的属性绑定到界面控件上,当属性值发生变化时,界面会自动更新。 4. 布局和自动排版:思维导图中的节点可能会很多,因此需要实现自动排版来保证节点的布局整齐美观。可以使用 WPF 的布局控件如网格(Grid)、堆栈面板(StackPanel)等进行节点的布局,并根据节点之间的关系自动调整节点的位置和大小。 5. 导出和导入:实现将思维导图保存为文件或导入文件的功能,可以使用 WPF 的文件操作功能来实现。可以将思维导图保存为 XML、JSON 或其他格式,并提供打开、保存功能供用户使用。 通过以上步骤,就可以使用 WPF 实现类似 XMind 的思维导图工具。当然,具体的实现过程和功能细节还需要根据实际需求进行具体设计和开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值