<i id='No9fl'><tr id='No9fl'><dt id='No9fl'><q id='No9fl'><span id='No9fl'><b id='No9fl'><form id='No9fl'><ins id='No9fl'></ins><ul id='No9fl'></ul><sub id='No9fl'></sub></form><legend id='No9fl'></legend><bdo id='No9fl'><pre id='No9fl'><center id='No9fl'></center></pre></bdo></b><th id='No9fl'></th></span></q></dt></tr></i><div id='No9fl'><tfoot id='No9fl'></tfoot><dl id='No9fl'><fieldset id='No9fl'></fieldset></dl></div>

<small id='No9fl'></small><noframes id='No9fl'>

  • <legend id='No9fl'><style id='No9fl'><dir id='No9fl'><q id='No9fl'></q></dir></style></legend>
          <bdo id='No9fl'></bdo><ul id='No9fl'></ul>

        <tfoot id='No9fl'></tfoot>
      1. 在控件外部单击时如何关闭 Silverlight 中的弹出窗口?

        时间:2023-09-16

          • <bdo id='fvLub'></bdo><ul id='fvLub'></ul>

              1. <legend id='fvLub'><style id='fvLub'><dir id='fvLub'><q id='fvLub'></q></dir></style></legend>
                  <tbody id='fvLub'></tbody>
                <i id='fvLub'><tr id='fvLub'><dt id='fvLub'><q id='fvLub'><span id='fvLub'><b id='fvLub'><form id='fvLub'><ins id='fvLub'></ins><ul id='fvLub'></ul><sub id='fvLub'></sub></form><legend id='fvLub'></legend><bdo id='fvLub'><pre id='fvLub'><center id='fvLub'></center></pre></bdo></b><th id='fvLub'></th></span></q></dt></tr></i><div id='fvLub'><tfoot id='fvLub'></tfoot><dl id='fvLub'><fieldset id='fvLub'></fieldset></dl></div>
                <tfoot id='fvLub'></tfoot>

              2. <small id='fvLub'></small><noframes id='fvLub'>

                • 本文介绍了在控件外部单击时如何关闭 Silverlight 中的弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的 Silverlight UI 中,我有一个按钮,单击该按钮会弹出一个带有一些过滤参数的控件.我希望这个控件在您单击它外部时隐藏起来.换句话说,它应该以类似于组合框的方式起作用,但它不是组合框(您不选择其中的项目).以下是我尝试捕获控件外部的单击以将其关闭的方式:

                  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; };
                      }
                  }
                  

                  不幸的是,MouseLeftButtonDown 的事件处理程序永远不会被触发.是否有一种成熟的方法可以制作弹出控件,当您在外部单击时自动关闭?如果没有,为什么我的 MouseLeftButtonDown 处理程序没有触发?

                  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?

                  解决方案:

                  我想我会发布我的整个解决方案,以防其他人发现它有帮助.在我的顶级视觉效果中,我为弹出窗口声明了一个盾牌",如下所示:

                  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>
                  

                  然后,我为 Popup 类添加了一个扩展方法,如下所示:

                  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();
                  }
                  

                  推荐答案

                  你给你的 RootVisual 设置了背景颜色吗?

                  Did you set a background color on your RootVisual?

                  这篇关于在控件外部单击时如何关闭 Silverlight 中的弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:帮助理解 .NET 委托、事件和事件处理程序 下一篇:如何在 Visual Studio 2008 中为 ASP.NET 添加页面事件

                  相关文章

                  <small id='PQPae'></small><noframes id='PQPae'>

                • <legend id='PQPae'><style id='PQPae'><dir id='PQPae'><q id='PQPae'></q></dir></style></legend>
                • <i id='PQPae'><tr id='PQPae'><dt id='PQPae'><q id='PQPae'><span id='PQPae'><b id='PQPae'><form id='PQPae'><ins id='PQPae'></ins><ul id='PQPae'></ul><sub id='PQPae'></sub></form><legend id='PQPae'></legend><bdo id='PQPae'><pre id='PQPae'><center id='PQPae'></center></pre></bdo></b><th id='PQPae'></th></span></q></dt></tr></i><div id='PQPae'><tfoot id='PQPae'></tfoot><dl id='PQPae'><fieldset id='PQPae'></fieldset></dl></div>
                  • <bdo id='PQPae'></bdo><ul id='PQPae'></ul>
                    <tfoot id='PQPae'></tfoot>