How to Use a Custom Control

RogerSchlueter-7899 1,526 Reputation points
2023-10-05T02:52:47.16+00:00

I am writng my first, really simple custom control just to ensure that I know how to make the parts work together. I have only added the Namespace "RSComboBox" to the default CustomControl1 that VisualStudio creates. However, the default Generic.xaml that VisualStudio creates now has some errors:

<ResourceDictionary
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:local="clr-namespace:RSComboBox">
	<Style TargetType="{x:Type local:CustomControl1}">
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate
					TargetType="{x:Type local:CustomControl1}">
					<Border
						Background="{TemplateBinding Background}"
						BorderBrush="{TemplateBinding BorderBrush}"
						BorderThickness="{TemplateBinding BorderThickness}">
						<Label Content="Hello, World" />
					</Border>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>
</ResourceDictionary>

The errors are from lines 4 and 5.:

  1. XLS0419 Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'RSComboBox' that could not be found.
  2. XHR0008 The name "CustomControl1" does not exist in the namespace "clr-namespace:RSComboBox".
  3. MC3050 Cannot find the type 'local:CustomControl1'. Note that type names are case sensitive.

How do I include the Namespace and yet not generate the errors?

Next, I removed the Namespace which allows a clean compile. Now I wrote a simple application to use this Custom Control. I added

*f:\RSComboBox\x64\Release\net7.0-windows\RSComboBox.dll*

to the project references so my MainWindow xaml is:

<Window
	x:Class="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:rs="clr-namespace:RSComboBox;assembly=RSComboBox"
	mc:Ignorable="d"
	Title="MainWindow" Height="250" Width="200">
	<rs:RSComboBox />

</Window>

This yields three error messages:

  1. XDG0008 The name "RSComboBox" does not exist in the namespace "clr-namespace:RSComboBox;assembly=RSComboBox".
  2. XLS0414 The type 'rs:RSComboBox' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
  3. Warning MC3074 The tag 'RSComboBox' does not exist in XML namespace 'clr-namespace:RSComboBox;assembly=RSComboBox'

Obviously, I am not linking this application and the custom control properly. How do I do that?

Developer technologies | Windows Presentation Foundation
Developer technologies | VB
{count} votes

Answer accepted by question author
  1. Hui Liu-MSFT 48,706 Reputation points Microsoft External Staff
    2023-10-09T08:46:17.9566667+00:00

    Hi,@Roger Schlueter. Not sure about your specific code. For questions about how to use custom controls correctly, you could try to refer to my solution.

    Here is a sample you can refer to:

    project structure:

    User's image

    
    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfCustomControlLibrary1">
        <Style TargetType="{x:Type local:CustomControl1}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                            <Label Content="Hello, World" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
    
    1. Create a new or open an existing WPF project.
    2. Right-click on the project, select "Add" > "Project Reference..."
    3. Browse and select the DLL file generated by your custom control library project (e.g., WpfCustomControlLibrary1.dll). User's image
    4. You could use references as follows in your senario:

    User's image

    xmlns:MyNamespace="clr-namespace:RSComboBox;assembly=RSComboBox"

    RSComboBox

    MainWindow.xaml:

    <Window x:Class="CustomControlUsing.MainWindow"
           ...
            xmlns:rs="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"
            xmlns:local="clr-namespace:CustomControlUsing"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <rs:CustomControl1 x:Name="customControl1" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="100" />
        </Grid>
    </Window>
    
    

    The result:

    e


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.