Why are you trying to pass a string in the constructor of the user control? This will only work if you are manually initializing the UserControl and adding to the main window - a one-shot deal.
A UserControl is just a class with UI functionality included, so it has the same rules of usage.
If you want to pass a value to a UserControl, add a public property or Method. Then you can pass values from the MainWindow to the UserControl - at any time for the life of the UserControl.
Here is a little demo for you.
NOTE: This is not how I would normally do this. However, I feel that this is as simple as I can make it for you.
The
MainWindow
has two UserControls:
*
SearchWidget
holds the search input functionality
*
DashboardWidget
Takes the Search and applies it.
The two controls communicate via the
MainWindow
.
1.
DashboardWidget
UserControl:
<UserControl x:Class="WpfControlBasicsDemo.DashboardWidget"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TextBlock x:Name="SearchText"
FontSize="32"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</UserControl>
and the code-behind:
public partial class DashboardWidget : UserControl
{
public DashboardWidget()
{
InitializeComponent();
}
public void DoSearch(string searchText)
{
SearchText.Text = searchText;
}
}
I have exposed a method to take the search text and do something with it.
2.
SearchWidget
UserControl:
<UserControl x:Class="WpfControlBasicsDemo.SearchWidget"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Content="Search:"
FontWeight="Bold"
Margin="0 0 10 0"/>
<TextBox Grid.Column="1"
x:Name="txtSearch" />
<Button Grid.Column="2"
Margin="10 0 0 0"
Content="Search"
Click="ButtonBase_OnClick"/>
</Grid>
</UserControl>
and the code-behind:
public partial class SearchWidget : UserControl, INotifyPropertyChanged
{
private string searchText;
public SearchWidget()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
SearchText = txtSearch.Text;
}
public string SearchText
{
get => searchText;
set
{
if (value == searchText) return;
searchText = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Here I do two things:
1. When the button is pressed, I set the public property
2. The public property fires an
OnPropertyChanged
event.
3.
MainWindow
This will host the two UserControls and pass the search text from one to the other:
<Window x:Class="WpfControlBasicsDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfControlBasicsDemo"
mc:Ignorable="d" Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition/>
</Grid.RowDefinitions>
<local:SearchWidget x:Name="SearchWidget"/>
<local:DashboardWidget x:Name="DashboardWidget" Grid.Row="1"/>
</Grid>
</Window>
and the code-behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SearchWidget.PropertyChanged += SearchWidget_PropertyChanged;
}
private void SearchWidget_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
DashboardWidget.DoSearch(SearchWidget.SearchText);
}
}
I listen for the
PropertyChanged
event from the
SearchWidget
UserControl and pass the text to the
DashboardWidget
UserControl via the public method
DoSearch
.