WPF弹出staysopen =虚假仍保持开放弹出的同时单击外 [英] WPF popup staysopen=false still keep the popup open while clicking outside

查看:86
本文介绍了WPF弹出staysopen =虚假仍保持开放弹出的同时单击外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我的问题是,我做了一个列表框弹出内,并设置弹出窗口的staysopen = FALSE。但每次弹出对话框弹出,我必须点击弹出里面的东西(如选择列表框中的元素),然后在弹出的外面,它会自动关闭。如果我没有点击任何东西,即使我点击弹出以外的其他元素,在弹出岿然不动。我需要弹出的关闭,而不需要我点击里面的任何元素。我能做什么?下面是代码,还有一些其他的风格连结此代码,但只是一些色彩风格。



我的控制是当用户点击弹出对话框顶部的文本框中,列表框弹出。如果用户不执行任何操作,然后单击该元素以外的任何地方,在弹出的对话框将关闭。谢谢你。



我可以使用下面的代码来获取它在Silverlight中完成。但似乎在WPF中,它不工作了。



popupAncestor = FindHighestAncestor(this.ListBoxPopup);
如果(popupAncestor == NULL)
{
的回报;
}
popupAncestor.AddHandler(System.Windows.Controls.Primitives.Popup.MouseLeftButtonDownEvent,(MouseButtonEventHandler)ClosePopup,真实);

 <电网X:NAME =MainGrid保证金=0VerticalAlignment =中心> 
< Grid.RowDefinitions>
< RowDefinition高度=20>< / RowDefinition>
< RowDefinition高度=自动>< / RowDefinition>
< /Grid.RowDefinitions>
<电网保证金=1,1,1,0X:NAME =顶栏能见度=可见Grid.Row =0HEIGHT =20VerticalAlignment =弹力的Horizo​​ntalAlignment =拉伸后台={StaticResource的COL_BTN_LIGHT}>
< Grid.ColumnDefinitions>
将; ColumnDefinition宽度=*>&下; / ColumnDefinition>
< ColumnDefinition WIDTH =19>< / ColumnDefinition>
< /Grid.ColumnDefinitions>
<文本框X:NAME =TextBoxSearchItemX:FieldModifier =私人的Horizo​​ntalAlignment =拉伸Grid.Column =0VerticalAlignment =拉伸了borderThickness =0,0,0,0的背景=透明框TextChanged =TextBoxSearchItem_TextChanged>< /文本框>
<切换按钮X:NAME =DropDownArrorButtonGrid.Column =1样式={StaticResource的ComboBoxReadonlyToggleButton}>< /切换按钮>
<! - < TextBlock中的Horizo​​ntalAlignment =中心文本=搜索Grid.ColumnSpan =2TextBlock.FontStyle =斜体不透明度=0.4VerticalAlignment =中心/> - - >
< /网格和GT;
<电网Grid.Row =1的Horizo​​ntalAlignment =弹力X:NAME =PopupGrid保证金=0,1,0,0>
<弹出X:NAME =ListBoxPopupStaysOpen =FALSEX:FieldModifier =私人ISOPEN ={绑定的ElementName = DropDownArrorButton,路径=器isChecked,模式=双向}
AllowsTransparency = 真正的保证金=0的Horizo​​ntalAlignment =拉伸布局=底
PlacementTarget ={绑定的ElementName =顶栏}打开=OnPopupOpened关=OnPopupClosed
Horizo​​ntalOffset ={绑定的ElementName = PopupGrid,路径=价值模式=双向}
VerticalOffset ={结合的ElementName = PopupGrid,路径=价值模式=双向}>
< ListBox的X:名称=ListBoxContainerWIDTH ={绑定的ElementName = MainGrid,路径= ActualWidth的}
Horizo​​ntalContentAlignment =弹力的SelectionMode =单HEIGHT =200保证金= 0
的SelectionChanged =ListBoxContainer_SelectionChanged
MouseDoubleClick =ListBoxContainer_MouseDoubleClick>
< ListBox.ItemTemplate>
<&DataTemplate的GT;
<电网的Horizo​​ntalAlignment =拉伸>
< BORDER BorderBrush ={结合SearchedBa​​ckColor}了borderThickness ={结合指示灯}WIDTH ={绑定的ElementName = MainGrid,路径= ActualWidth的}>
< TextBlock的X:名称=ContentText文本={绑定名称}保证金=1,0,0,0/>
< /边框>
< /网格和GT;
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
< /弹出>
< BORDER X:NAME =listBorderBorderBrush ={StaticResource的COL_BTN}了borderThickness =0,1,0,0>< /边框>
< /网格和GT;
< /网格和GT;


解决方案

你应该在你的视图模型创建一个依赖属性或如下图所示的IsPopupOpen控制来管理弹出的状态。然后,你可以绑定两个切换按钮器isChecked和弹出ISOPEN到DP。



在您的切换按钮,将可调焦=假和IsThreeState = FALSE

另外公共BOOL IsDropDownOpen 
{
{返回(布尔)的GetValue(IsDropDownOpenProperty); }
集合{的SetValue(IsDropDownOpenProperty,值); }
}

公共静态只读的DependencyProperty IsDropDownOpenProperty =
DependencyProperty.Register(IsDropDownOpen的typeof(布尔)的typeof(窗口),新UIPropertyMetadata(假));



祝你好运!


my problem here is I made a listbox inside the popup, and set the popup's staysopen=false. But each time popup box pops, I have to click something inside the popup(like select an element in listbox), then click outside the popup, and it will close automatically. If I don't click anything, and even if I click other elements outside the popup, the popup stays on. I need the popup closes without needing me to click any element inside it. What can I do? Here is the code, there are some other style link to this code but just some color style.

My control is when user click the textbox on the top of the popup box, the listbox pops. If user does nothing and click any place outside this element, the popup box closes. Thanks.

I can use the following code to get it done in silverlight. But seems like in wpf, it is not working anymore.

popupAncestor = FindHighestAncestor(this.ListBoxPopup); if (popupAncestor == null) { return; } popupAncestor.AddHandler(System.Windows.Controls.Primitives.Popup.MouseLeftButtonDownEvent, (MouseButtonEventHandler)ClosePopup, true);

<Grid x:Name="MainGrid" Margin="0" VerticalAlignment="Center">
    <Grid.RowDefinitions>
        <RowDefinition Height="20"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid Margin="1,1,1,0" x:Name="TopBar"  Visibility="Visible"  Grid.Row="0" Height="20" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="{StaticResource COL_BTN_LIGHT}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="19"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBox x:Name="TextBoxSearchItem"  x:FieldModifier="private" HorizontalAlignment="Stretch" Grid.Column="0" VerticalAlignment="Stretch" BorderThickness="0,0,0,0" Background="Transparent" TextChanged="TextBoxSearchItem_TextChanged"></TextBox>
        <ToggleButton x:Name="DropDownArrorButton" Grid.Column="1" Style="{StaticResource ComboBoxReadonlyToggleButton}"></ToggleButton>
        <!--<TextBlock HorizontalAlignment="Center" Text="Search" Grid.ColumnSpan="2" TextBlock.FontStyle="Italic" Opacity="0.4" VerticalAlignment="Center"/>-->
    </Grid>
    <Grid Grid.Row="1" HorizontalAlignment="Stretch" x:Name="PopupGrid"  Margin="0,1,0,0" >
        <Popup x:Name="ListBoxPopup" StaysOpen="False" x:FieldModifier="private"  IsOpen="{Binding ElementName=DropDownArrorButton, Path=IsChecked, Mode=TwoWay}" 
               AllowsTransparency="true" Margin="0" HorizontalAlignment="Stretch" Placement="Bottom" 
               PlacementTarget="{Binding ElementName=TopBar}"  Opened="OnPopupOpened" Closed="OnPopupClosed"
               HorizontalOffset="{Binding ElementName=PopupGrid, Path=Value, Mode=TwoWay}"
                 VerticalOffset="{Binding ElementName=PopupGrid, Path=Value, Mode=TwoWay}">
            <ListBox x:Name="ListBoxContainer" Width="{Binding ElementName=MainGrid, Path=ActualWidth}" 
                     HorizontalContentAlignment="Stretch" SelectionMode="Single"  Height="200"  Margin="0" 
                     SelectionChanged="ListBoxContainer_SelectionChanged"
                     MouseDoubleClick="ListBoxContainer_MouseDoubleClick">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid HorizontalAlignment="Stretch">
                            <Border BorderBrush="{Binding SearchedBackColor}" BorderThickness="{Binding Indicator}" Width="{Binding ElementName=MainGrid, Path=ActualWidth}">
                                <TextBlock x:Name="ContentText" Text="{Binding Name}" Margin="1,0,0,0"/>
                            </Border>                               
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Popup>                
        <Border x:Name="listBorder" BorderBrush="{StaticResource COL_BTN}" BorderThickness="0,1,0,0" ></Border>     
    </Grid>                 
</Grid>

解决方案

You should create a dependency property in your view model or control for "IsPopupOpen" as shown below to manage state of the popup. Then you can bind both the ToggleButton "IsChecked" and popup "IsOpen" to that DP.

Also on your ToggleButton, set "Focusable=false" and "IsThreeState=false"

    public bool IsDropDownOpen
    {
     get { return (bool)GetValue(IsDropDownOpenProperty); }
     set { SetValue(IsDropDownOpenProperty, value); }
    }        

    public static readonly DependencyProperty IsDropDownOpenProperty =
          DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(Window), new UIPropertyMetadata(false));

Good luck!

这篇关于WPF弹出staysopen =虚假仍保持开放弹出的同时单击外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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