Reputation: 23
Lets say I have a user control such as this:
<UserControl
x:Class="WPFUserControl.MyUserControl"
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"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Orientation="Horizontal">
<TextBox x:Name="txtName" />
<Button
x:Name="btnClickMe"
Content="Greet Me" />
</StackPanel>
</UserControl>
and a Window like this:
<Window
x:Class="XAMLUserControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:control="clr-namespace:WPFUserControl"
Title="MainWindow" Height="600" Width="800">
<Grid>
<control:MyUserControl/>
</Grid>
</Window>
Is there a way I can refer to the user control without using the control:
prefix?
So, instead of writing
<control:MyUserControl/>
I would use
<MyUserControl/>
Upvotes: 2
Views: 689
Reputation: 2370
You can use the XmlnsDefinitionAttribute to make the namespace containing MyUserControl
"belong" to the same xml namespace (something like this : http://schemas.microsoft.com/winfx/2006/xaml/presentation, basically the value assigned to xmlns
itself in your UserControl's xaml) as all native wpf components. That should let you use your UserControl without having to use a namespace prefix.
Usage for your example would be in AssemblyInfo.cs, doing [assembly:XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "WPFUserControl")]
.
The first paramater is the xml namespace that will be used and the second parameter is the c# namespace that contains the things you want to use without prefix in xaml.
You can find more info about how XAML and C# namespaces work together here
Upvotes: 2