Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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>
Posted
Updated 17-Oct-17 4:55am

1 solution

I can think of 3 different ways to do this:

1. DataBinding:

UReceipt - XAML
XML
<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>

UReceipt - Code-behind
C#
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
XML
<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
C#
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. Passing a reference of UReceipt to USearch:

UReceipt - XAML
XML
<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
C#
public partial class UReceipt : UserControl
{
    public UReceipt()
    {
        InitializeComponent();
        Search.Host = this;
    }

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

USearch - XAML
XML
<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
C#
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
XML
<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
C#
public partial class UReceipt : UserControl
{
    public UReceipt()
    {
        InitializeComponent();
    }

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

USearch - XAML
XML
<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
C#
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
C#
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;
    }
}
 
Share this answer
 
v2
Comments
Richard Deeming 17-Oct-17 12:34pm    
I'm starting to think you've got far too much time on your hands! :D
Graeme_Grant 17-Oct-17 20:46pm    
I work with WPF all day every day ATM. It took longer to post the solution than to write the code (copy, paste, modify)... ;)
mahdiiiiyeh 18-Oct-17 2:37am    
Thank you very much, dear friend for the solutions that you made.
Good luck.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900