Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF User-controls
I created and compiled a WPF UserControl Library using VS 2008 .NET 3.5 on a Vista machine.
Then I created a simple WPF Windows application. I added a reference to the compiled UserControl Library. Now when I try to a namespace in the XMAL code for the window I do not see my compiled UserControl namespace.
What am I missing?
Posted 27-Nov-10 7:57am
Edited 27-Nov-10 9:36am
Henry Minute223.8K
v3
Comments
JF2015 at 27-Nov-10 12:59pm
   
Edited to remove <pre> tag since they are supposed to be used for code only.
Henry Minute at 27-Nov-10 13:31pm
   
Did you add a namespace reference (xmlns) in your .xaml file?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

After you've added the reference, you can call the UserControl directly from the code behind, like this:
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Content = new WpfControlLibrary1.UserControl1();
        }
    }
 
If you want to use it from the XAML code, use this:
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:lib="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <lib:UserControl1 />
    </Grid>
</Window>
 
The xmlns:lib attribute of the Window element, maps the xml namespace lib to the WpfControlLibrary1 namespace. This is the syntax:
xmlns:{name}="clr-namespace:{namespace};assembly={assembly}"
 
I hope this helped you!
 
PS:
remember to recompile the whole solution, otherwise, the IntelliSense won't be able to detect the new namespace!
  Permalink  
Comments
Tarun.K.S at 28-Nov-10 1:43am
   
Good Answer!
69Icaro at 28-Nov-10 5:11am
   
Thanks! I'm very happy when I can help people!
Tarun.K.S at 28-Nov-10 12:33pm
   
Great! have a 5!
69Icaro at 29-Nov-10 16:20pm
   
Thanks!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thanks 691caro and the rest. I have done exactly what 691caro suggested and it doesn't work. my compiled dll is external to the current solution. If I run the app, it works. But the problem is that I do not see my namespace to insert it in the xaml file. is that normal behaviour? and also, as soon as I manually add the namespace the designer complains that I cannot load, is this a problem with visual studio 2008.
Also, do you guys have or can direct me where I can take a look at some smaple code showing the use of a UserControl using data binding to a business object, say a person object, and which uses DependencyProperty properties? The examples I have seen about UserControls and databinding using DependencyProperty properties 99% of them only have one property. I would like to see one that uses a customer or a person object, something more realistic.

Thank you in advance. I really appreciate it.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,078
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 28 Nov 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100