如何单击控制之外解雇时,在Silverlight中弹出? [英] How to dismiss a popup in Silverlight when clicking outside of the control?

查看:199
本文介绍了如何单击控制之外解雇时,在Silverlight中弹出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Silverlight UI,我有一个按钮,点击时会弹出一些过滤参数的控制。我想这种控制可以隐藏自身,当你点击它的外面。换句话说,它应该类似于组合框的方式发挥作用,但它不是一个组合框(你没有它选择一项)。以下是我正在试图捕捉单击控件之外予以驳回:

 公共部分类MyPanel:用户控件
{
公共MyPanel()
{
的InitializeComponent();
}

私人无效FilterButton_Click(对象发件人,RoutedEventArgs E)
{
//切换过滤弹出$ B $的开放状态B FilterPopup.IsOpen = !FilterPopup.IsOpen;
}

私人无效UserControl_Loaded(对象发件人,RoutedEventArgs E)
{
//捕获所有点击和关闭弹出
App.Current.RootVisu​​al .MouseLeftButtonDown + = {委托
FilterPopup.IsOpen = FALSE; };
}
}



不幸的是,事件处理程序的MouseLeftButtonDown 永远不会被解雇。有没有做,当你点击它外面的自动驳回弹出控制的一个行之有效的方法是什么?如果不是,为什么不是我的的MouseLeftButtonDown 处理烧成



解决方案:



我想我会后我的整个解决方案的情况下,其他人发现它有用。在我的顶级视觉,我宣布了弹出一个挡箭牌,是这样的:

 <用户控件的xmlns:我=CLR的命名空间:命名空间
X:类=Namespace.MainPage
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns :X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
的xmlns:导航=CLR的命名空间:System.Windows.Controls;装配= System.Windows.Controls.Navigation
的xmlns:uriMapper =CLR的命名空间:System.Windows.Navigation;装配= System.Windows.Controls.Navigation
的xmlns:D =htt​​p://schemas.microsoft.com/expression/blend / 2008年
的xmlns:MC =http://schemas.openxmlformats.org/markup-compatibility/2006
>
<网格背景=黑的Horizo​​ntalAlignment =拉伸
VerticalAlignment =拉伸>
<我:的MyStuff />
<帆布的Horizo​​ntalAlignment =拉伸VerticalAlignment =拉伸
X:NAME =PopupShield背景=透明WIDTH =自动
高度=自动能见度=坍塌/>
< /网格和GT;
< /用户控件>



然后,我添加了弹出一个扩展方法类,如下:

 公共静态类PopupUtils 
{
公共静态无效MakeAutoDismissing(这弹出弹出)
{
VAR盾=(App.Current.RootVisu​​al为的MainPage).PopupShield;

//每当弹出打开,部署屏蔽
popup.HandlePropertyChanges(
ISOPEN,
(S,E)=>
{
shield.Visibility =(布尔)e.NewValue
Visibility.Visible:Visibility.Collapsed;
}
);

//每当点击屏蔽,关闭该弹出
shield.MouseLeftButtonDown + =(S,E)=> popup.IsOpen = FALSE;
}
}

公共静态类FrameworkUtils
{
公共静态无效HandlePropertyChanges(
此FrameworkElement元素,弦乐propertyName的,
PropertyChangedCallback回调)
{
//绑定到depedency属性
结合b =新的Binding(propertyName的){源=元};
VAR道具= System.Windows.DependencyProperty.RegisterAttached(
ListenAttached+ propertyName的,
的typeof(对象),
typeof运算(用户控件),
新系统。 Windows.PropertyMetadata(回调));

element.SetBinding(道具,B);
}
}



扩展方法使用这样的:

 私人无效UserControl_Loaded(对象发件人,RoutedEventArgs E)
{
FilterPopup.MakeAutoDismissing();
}


解决方案

你设置背景颜色您RootVisu​​al?


In my Silverlight UI, I have a button that when clicked pops up a control with some filtering parameters. I would like this control to hide itself when you click outside of it. In other words, it should function in a manner similar to a combo box, but it's not a combo box (you don't select an item in it). Here's how I'm trying to capture a click outside of the control to dismiss it:

public partial class MyPanel : UserControl
{
    public MyPanel()
    {
        InitializeComponent();
    }

    private void FilterButton_Click(object sender, RoutedEventArgs e)
    {
        // Toggle the open state of the filter popup
        FilterPopup.IsOpen = !FilterPopup.IsOpen;
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        // Capture all clicks and close the popup
        App.Current.RootVisual.MouseLeftButtonDown += delegate {
            FilterPopup.IsOpen = false; };
    }
}

Unfortunately, the event handler for MouseLeftButtonDown is never getting fired. Is there a well-established way of making a popup control that auto-dismisses when you click outside of it? If not, why isn't my MouseLeftButtonDown handler firing?

Solution:

I thought I'd post my entire solution in case others find it helpful. In my top-level visual, I declare a "shield" for the popups, like this:

<UserControl xmlns:my="clr-namespace:Namespace"
    x:Class="Namespace.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
    xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
>
  <Grid Background="Black" HorizontalAlignment="Stretch" 
          VerticalAlignment="Stretch">
    <my:MyStuff/>
    <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
            x:Name="PopupShield" Background="Transparent" Width="Auto" 
            Height="Auto" Visibility="Collapsed"/>
  </Grid>
</UserControl>

Then, I added an extension method for the Popup class, like this:

public static class PopupUtils
{
    public static void MakeAutoDismissing(this Popup popup)
    {
        var shield = (App.Current.RootVisual as MainPage).PopupShield;

        // Whenever the popup opens, deploy the shield
        popup.HandlePropertyChanges(
            "IsOpen",
            (s, e) =>
            {
                shield.Visibility = (bool)e.NewValue 
                    ? Visibility.Visible : Visibility.Collapsed;
            }
        );

        // Whenever the shield is clicked, dismiss the popup
        shield.MouseLeftButtonDown += (s, e) => popup.IsOpen = false;
    }
}

public static class FrameworkUtils
{
    public static void HandlePropertyChanges(
        this FrameworkElement element, string propertyName, 
        PropertyChangedCallback callback)
    {
        //Bind to a depedency property
        Binding b = new Binding(propertyName) { Source = element };
        var prop = System.Windows.DependencyProperty.RegisterAttached(
            "ListenAttached" + propertyName,
            typeof(object),
            typeof(UserControl),
            new System.Windows.PropertyMetadata(callback));

        element.SetBinding(prop, b);
    }
}

The extension method is used like this:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    FilterPopup.MakeAutoDismissing();
}

解决方案

Did you set a background color on your RootVisual?

这篇关于如何单击控制之外解雇时,在Silverlight中弹出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆