在用户控件WPF中关闭弹出窗口 [英] Close popup in user control WPF

查看:85
本文介绍了在用户控件WPF中关闭弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友

我有一个用户控件(UReceipt.xaml),弹出窗口打开。弹出窗口是用户控件(USearch.xaml)(包括一个列表视图和几个按钮)。

现在我想双击弹出窗口列表视图中的行并关闭用户控件。然后返回控制用户(UReceipt.xaml )

请指导我

谢谢。



我尝试过:



< Popup x:Name =ppPlacement =Center>

< local:usearch>

Hello friends
I have a user control (UReceipt.xaml),in which a pop-up opens.Inside the pop-up is the user control(USearch.xaml) (which includes a list view and several buttons).
Now I want to double-click on the row in the list view of pop-ups and close the user control.And go back to the control user(UReceipt.xaml)
please guide me
Thank you.

What I have tried:

<Popup x:Name="pp" Placement="Center" >
<local:usearch>

推荐答案

我可以想到3种不同的方法:



1.数据绑定:



UReceipt - XAML

I can think of 3 different ways to do this:

1. DataBinding:

UReceipt - XAML
<UserControl

    x:Class="Popup.UReceipt"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="clr-namespace:Popup">

    <Grid>
        <Button HorizontalAlignment="Center"

                VerticalAlignment="Center"

                Padding="10 5"

                Click="Button_Click">
            <Button.Content>
                <Grid>
                    <TextBlock Text="Click Me"/>
                    <Popup IsOpen="{Binding ShowPopup}"

                           StaysOpen="False">
                        <local:USearch x:Name="Search"/>
                    </Popup>
                </Grid>
            </Button.Content>
        </Button>
    </Grid>

</UserControl>



< b> UReceipt - 代码隐藏


UReceipt - Code-behind

public partial class UReceipt : UserControl, INotifyPropertyChanged
{
    public UReceipt()
    {
        InitializeComponent();
        DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private bool showPopup;
    public bool ShowPopup
    {
        get { return showPopup; }
        set
        {
            showPopup = value;
            PropertyChanged?.Invoke(this,
                                    new PropertyChangedEventArgs(nameof(ShowPopup)));
        }
    }


    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ShowPopup = true;
    }
}



USearch - XAML


USearch - XAML

<UserControl

    x:Class="Popup.USearch"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Width="100" Height="200" Background="AliceBlue">
        <TextBlock Text="Search"/>
        <Button Content="CLOSE" Padding="10 5"

                Click="Button_Click"

                HorizontalAlignment="Center"

                VerticalAlignment="Center" />
    </Grid>

</UserControl>



USearch - 代码隐藏


USearch - Code-behind

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

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        (DataContext as UReceipt).ShowPopup = false;
        e.Handled = true;
    }
}



2.将 UReceipt 的引用传递给 USearch



UReceipt - XAML


2. Passing a reference of UReceipt to USearch:

UReceipt - XAML

<UserControl

    x:Class="Popup.UReceipt"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="clr-namespace:Popup">

    <Grid>
        <Button HorizontalAlignment="Center"

                VerticalAlignment="Center"

                Padding="10 5"

                Click="Button_Click">
            <Button.Content>
                <Grid>
                    <TextBlock Text="Click Me"/>
                    <Popup x:Name="SearchPopup"

                           StaysOpen="False">
                        <local:USearch x:Name="Search"/>
                    </Popup>
                </Grid>
            </Button.Content>
        </Button>
    </Grid>

</UserControl>



UReceipt - 代码隐藏


UReceipt - Code-behind

public partial class UReceipt : UserControl
{
    public UReceipt()
    {
        InitializeComponent();
        Search.Host = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SearchPopup.IsOpen = true;
    }
}



USearch - XAML


USearch - XAML

<UserControl

    x:Class="Popup.USearch"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Width="100" Height="200" Background="AliceBlue">
        <TextBlock Text="Search"/>
        <Button Content="CLOSE" Padding="10 5"

                Click="Button_Click"

                HorizontalAlignment="Center"

                VerticalAlignment="Center" />
    </Grid>

</UserControl>



USearch - 代码隐藏


USearch - Code-behind

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

    private UReceipt host;
    public UReceipt Host { set { host = value; } }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        host.SearchPopup.IsOpen = false;
        e.Handled = true;
    }
}



3. Visual Parent reference:



UReceipt - XAML


3. Visual Parent reference:

UReceipt - XAML

<UserControl

    x:Class="Popup.UReceipt"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="clr-namespace:Popup">

    <Grid>
        <Button HorizontalAlignment="Center"

                VerticalAlignment="Center"

                Padding="10 5"

                Click="Button_Click">
            <Button.Content>
                <Grid>
                    <TextBlock Text="Click Me"/>
                    <Popup x:Name="SearchPopup"

                           StaysOpen="False">
                        <local:USearch x:Name="Search"/>
                    </Popup>
                </Grid>
            </Button.Content>
        </Button>
    </Grid>

</UserControl>



UReceipt - Code-behind


UReceipt - Code-behind

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

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SearchPopup.IsOpen = true;
    }
}



USearch - XAML


USearch - XAML

<UserControl

    x:Class="Popup.USearch"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Width="100" Height="200" Background="AliceBlue">
        <TextBlock Text="Search"/>
        <Button Content="CLOSE" Padding="10 5"

                Click="Button_Click"

                HorizontalAlignment="Center"

                VerticalAlignment="Center" />
    </Grid>

</UserControl>



USearch - Code-behind


USearch - Code-behind

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

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.GetVisualParent<Popup>().IsOpen = false;
        e.Handled = true;
    }
}



Helper Extension


Helper Extension

public static class ParentOfTypeExtensions
{
    public static T ParentOfType<T>(this DependencyObject element)
        where T : DependencyObject
    {
        if (element == null) return null;
        return element.GetParents().OfType<T>().FirstOrDefault();
    }

    public static T GetVisualParent<T>(this DependencyObject element)
        where T : DependencyObject
    {
        return element.ParentOfType<T>();
    }

    public static T GetParent<T>(this DependencyObject element)
        where T : FrameworkElement
    {
        return element.ParentOfType<T>();
    }

    public static IEnumerable<DependencyObject>
        GetParents(this DependencyObject element)
    {
        if (element == null)
            throw new ArgumentNullException("element");

        while ((element = element.GetParent()) != null)
            yield return element;
    }

    public static DependencyObject GetParent(this DependencyObject element)
    {
        DependencyObject parent = null;
        try
        {
            parent = VisualTreeHelper.GetParent(element);
        }
        catch (InvalidOperationException)
        {
            parent = null;
        }

        if (parent == null)
        {
            if (element is FrameworkElement frameworkElement)
                parent = frameworkElement.Parent;

            if (element is FrameworkContentElement frameworkContentElement)
                parent = frameworkContentElement.Parent;
        }
        return parent;
    }
}


这篇关于在用户控件WPF中关闭弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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