Here are the steps to do what you ask:
1. In the Solution Explorer, right-click on your solution > Add > New Project & select "WPF UserControl Library (.Net Framework), give it a name and click on the OK button.
2. Now Add a UserControl with a given name. Pretty it up with Xaml and add functionality with code then compile.
3. Add a reference in the project that wants to use it.
4. Then in your Xaml, add a Namespace reference to the UserControl in your new external UserControl library
5. Add the UserControl to your Xaml.
If you want to see how it is done with code, then have a look at this article:
Flexible WPF ToggleSwitch Lookless Control in C# & VB[
^] - not a UserControl but same principle (as described above) applies.
UPDATE Threw this together whilst having lunch:
1.
UserControlLib to hold our shared UserControls. Here is your UserControl called
MySharedUserControl:
<UserControl
x:Class="UserControlLib.MySharedUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Margin="10">
<Border Background="GhostWhite" BorderBrush="Gainsboro" BorderThickness="1">
<StackPanel Margin="10">
<TextBlock Text="{Binding Message}" />
</StackPanel>
</Border>
</Grid>
</UserControl>
2. Our test App
WpfApp1 with a reference added to our
UserControlLib UserControl lib:
Code-Behind for the MainWindow:
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private string message;
public string Message
{
get { return message; }
set
{
message = value;
RaisePropertyChanged();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Message = "Test Message";
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
And the Xaml:
<Window
x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyLib="clr-namespace:UserControlLib;assembly=UserControlLib"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Show Message" Click="Button_Click"
VerticalAlignment="Top" HorizontalAlignment="Center"
Margin="20" Padding="10 5"/>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="MESSAGE:" Margin="0 0 10 0" VerticalAlignment="Center"/>
<MyLib:MySharedUserControl Width="200" Grid.Column="1"/>
</Grid>
</Grid>
</Window>
Our UserControl Lib and the control NameSpace is added to the header:
xmlns:MyLib="clr-namespace:UserControlLib;assembly=UserControlLib"
and the control is used with our NameSpace:
<MyLib:MySharedUserControl Width="200" Grid.Column="1"/>