Create a new WPF project.
Add the following class:
internal static class PowerSettingsContext
{
public static Func<string, string> Find { get; set; }
}
This class will hold a delegate reference to the method in the UserControl that contains the
StackPanel
with the controls to search.
Here is that UserControl:
<UserControl x:Class="WpfSharedContext.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfSharedContext"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<StackPanel x:Name="MyStackPanel" Orientation="Vertical">
<TextBlock x:Name="TextBlock1" Text="John"/>
<TextBlock x:Name="TextBlock2" Text="Paul"/>
<TextBlock x:Name="TextBlock3" Text="George"/>
<TextBlock x:Name="TextBlock4" Text="Ringo"/>
</StackPanel>
</Grid>
</UserControl>
And the code-behind:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
PowerSettingsContext.Find = FindControl;
}
public string FindControl(string text)
{
foreach (TextBlock child in MyStackPanel.Children.Cast<TextBlock>())
{
if (child.Text.Equals(text,
StringComparison.InvariantCultureIgnoreCase))
return child.Name;
}
return "";
}
}
The constructor points the shared
PowerSettingsContext.Find
method to the search method in the UserControl:
PowerSettingsContext.Find = FindControl;
Now we can add the UserControl that will request the information:
<UserControl x:Class="WpfSharedContext.UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfSharedContext"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Margin="10"
Orientation="Horizontal"
Height="40">
<TextBlock Text="Enter Value:"
VerticalAlignment="Center"/>
<TextBox x:Name="SearchText" Width="100" Margin="10 0"/>
<Button x:Name="SearchButton"
Padding="20 10"
Content="GO!"
Click="SearchButton_OnClick"/>
</StackPanel>
<StackPanel Grid.Row="1" Margin="10" Orientation="Horizontal">
<TextBlock Text="Result:"/>
<TextBlock x:Name="Result" Width="100" Margin="10 0 0 0"/>
</StackPanel>
</Grid>
</UserControl>
and the code-behind:
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
}
private void SearchButton_OnClick(object sender, RoutedEventArgs e)
{
Result.Text = PowerSettingsContext.Find(SearchText.Text);
}
}
The Search Button Click will call the shared
PowerSettingsContext.Find
method and display the result. Here I am looking for the text and returning the control name that has the exact match.
Now we can add both UserControls to our host (window):
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<local:UserControl1 />
<local:UserControl2 Grid.Row="1" />
</Grid>
Now if you use any of the 4 names from UserControl1`, UserControl2 will show the control name that contains that text. Neither UserControl knows about the other, only the shared
PowerSettingsContext
class.