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>
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
<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
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
<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
public partial class UReceipt : UserControl
{
public UReceipt()
{
InitializeComponent();
Search.Host = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SearchPopup.IsOpen = true;
}
}
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
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
<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
public partial class UReceipt : UserControl
{
public UReceipt()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SearchPopup.IsOpen = true;
}
}
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
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
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;
}
}