I have created the WPF user control and WPF window in class library project. Controls are public.
These controls are available for other project of the same solution. I can see them in the toolbox of the VS2010 when i add project reference from the WPF application project.
However, when i create some WPF application project outside the solution and add reference to the my user control library project, the controls do not appear in the toolbox! (My applications XAML file is open when I try to see them in toolbox to drag-drop on the application's main XAML)
What makes the controls avilable to the outside world when their assembly is referenced by the consumers?
Try right clicking on the toolbox and selecting Choose Items From that form click browse and select your DLL. That should put the controls in the toolbox for you.
I think the problem is that in your new WPF project you need to set an XML namespace where you reference your user control library. Then you can use your controls in the XAML.
For example :
thats an xml namespace definition in the XAML:
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;
assembly=Microsoft.Phone.Controls.Toolkit"
And how you can use a control from this control library
< toolkit:ListPicker />
I hope this will help you.
Right click inside the toolbox and select "Show All".
Related
I have a class library that I created using the "Class Library" project template. If I right-click on this library in Solution Explorer and select "Add > User Control", Visual Studio adds a WinForms UserControl. That's not what I want -- I want "Add > User Control" to add a WPF user control.
I've already added references to the WPF assemblies (WindowsBase, PresentationFramework, and PresentationCore), and I already have some WPF UserControls in this library, and everything compiles. My library does not have references to the WinForms assemblies (System.Drawing and System.Windows.Forms). But apparently the proper references are not enough of a clue for Visual Studio, because when I try Add > User Control, it adds the WinForms references to my project, and then creates a WinForms UserControl.
I can add a WPF User Control to my WPF Application project, and then move it into my library. But that's a pain, and I'd rather have it work properly in the first place.
I think I'm probably missing some kind of arcane XML element in my .csproj file that tells Visual Studio which designer to use by default, and if I add the right XML element with the right cryptic GUID, it will start working properly. If I could create a new WPF Control Library, I could probably compare the two project files and figure this out. However, I'm using Visual C# Express, which doesn't have a template for a WPF Control Library project, so I'm out of luck there.
What do I need to do to my Class Library's .csproj file so that VS2010's Add > New User Control will add a WPF UserControl?
There are sub-projects class ids in the project file that affect the Visual Studio context menus and how the project behaves in general. The easiest thing to do is to recreate the project as a:
WPF User Control Library
instead of a "Class Library". It is possible if you already created the project to edit in the sub-project class ids by hand by opening the ".csproj" file in a text editor such as Visual Studio itself but its easy to cause more damage than you fix that way.
I believe but haven't test that another type of library will also work:
WPF Custom Control Library
which is intended to hold other types of controls than UserControl objects but being a WPF sub-project type the context menus also work correctly for the use case you are describing.
Edit:
For completeness, I've just tested how to manually add the sub-project GUIDS. Add this line to the first PropertyGroup in the .csproj file:
<PropertyGroup>
...
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
Not tested with Visual Studio Express.
If I create a WPF user control, it appears automatically in the Visual Studio toolbox.
Is there a way to make other controls appear automatically in the Visual Studio toolbox?
Conversely, is there a way to hide a user control from the toolbox?
You can right click on the toolbox and select Choose Items... Then from there you will get a dialog that allows you to select which controls to show or hide.
Per comment below if you are trying to figure out how to do this for a Third Party that you are providing your control to check out this MSDN article that describes packaging your control:
http://msdn.microsoft.com/en-us/library/ms165358.aspx
Auto population is described here (at the end), but to summarize you can add DesignTimeVisible(false) to your UserControl to prevent it from being added to the Toolbox.
Your controls should be added just like your UserControls, assuming they meet the requirements at the end of the link above, which are:
To appear in the Auto-Population
Toolbox process a type must derive
from FrameworkElement and:
Are public and have a default public or internal constructor or are
internal and have either a default
public or internal constructor
Types deriving from Window or Page are ignored
FrameworkElements in other .exe projects are ignore
Internal classes will only be displayed when the active designer is
for an item in the same project
Friend Assemblies are not taken into account for Toolbox
Auto-Population
If you are building reusable controls (where your end-users will simply add a reference to your assembly), then you'd need to tell Visual Studio that it should load your controls into the Toolbox. There is a tutorial for WinForms controls on doing this here, but the concepts are the same. A VSIX installer tutorial can be found here.
A lot of the resources out there are for older versions of Visual Studio, but again the same concepts should apply. You simply need to update version information where appropriate.
Is there a reason why Visual Studio won't let me create a WPF window in a DLL project?
I "solved" it by creating a window in an Application Project and copying it to my DLL project.
I also found that I could just create a UserControl and change the base class to "Window".
But if I had to do it this way, it's maybe because I shouldn't do it...
Make sure the project type is WPF User Control Library when you create your project.
If it isn't then no sweat, just edit the csproj file and make sure the <ProjectTypeGuids> element under Project/PropertyGroup contain the following GUIDs
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Also, make sure you reference PresentationFramework and System.Xaml in your project, or you will not get any WPF in your code.
You can try adding new WPF User Control Item and change that to Window.
Add New Item->WPF->User Control
In XAML:
Change <UserControl> tag as <Window>
In CS:
Change base class from System.Windows.Controls.UserControl to System.Windows.Window.
I do it this way:
1) create "WPF Application"
2) remove App.xaml
3) change Project properties -> Application Output type: to Class Library (originally there is Windows Application)
Otherwise you will get errors:
"Library project file cannot specify ApplicationDefinition element"
"The project file contains a property value that is not valid"
What do you mean that Visual Studio won't let you create a WPF window in a DLL project? Do you mean that if you right click the project, there is no option to add a Window there?
If that is the case, I think that means that you created a project type that isn't a WPF project type. I encountered something similar a while back when I wanted to upgrade a WinForms project to use WPF instead - see this question for more information.
I have a project with 100's of usercontrols. When I load the project in VS2010 and try to open a designer, the toolbox spins and spins until they're all loaded.
Winforms has a "AutoToolboxPopulate" switch under Tools/Options/Winddows Forms Designer/General.
I cannot find a similar switch for the XAML designer. Does one exist?
In VS2010, the Auto Populate Toolbox switch for XAML can be found under Tools/Options/Text Editor/XAML/Miscellaneous.
If you wish to keep the Auto Populate enabled, you can decorate your usercontrol classes with the System.ComponentModel.DesignTimeVisibleAttribute which will allow you to specify if they appear or not in the designer.
Unfortunately there isn't one that I am aware of, with the exception of actually adding a design-time (*.Design.dll) assembly for the project that effectively defines metadata to hide the explicit controls.
This bit me as well recently and I wish I had a solution like the old winforms attribute!
I'm building custom control for my Silverlight 2 app. It's in one SL class project, and it contains two files:
MyControl class, inherited from Control, with few DepedencyProperties
themes/generic.xaml, with visual elements (ControlTemplate), states for VSM and transitions
I created whole xaml by hand, and it works, but want to use Blend2(SP1) for editing! When i open generic.xaml in Blend, and switch to Resources tab I don't have anything to edit.
For example, when I put that visual template and states definition to App.xaml (of my main SL project), I can access all elements and States through Resources and States tabs, and edit them visually.
Does Blend even support editing generic.xaml from SL2 class project?
What's the best practice for building custom controls? I don't want to my custom control depends on anything from main SL2 project, but want them to be skinnable, and be able to change skins (themes) dynamically.
You can edit this with Blend.
Open your controls project in Blend.
Open your generic.xaml
click the Resources tab
Expand generic.xaml
Double click the style resource you want to edit.
In the Objects and Timeline section, right click the Style and select "Edit Control Parts -> Edit Template"
Now you can edit the template in the generic.xaml. Sounds like you're already following best practices by having the parts and states. If you want the full blown best practices take a look at this detailed post on how to deal with design time extensibility. There you will find out how to do the Visual Studio and Blend design time stuff for Silverlight.
I recreated whole project, and suddenly can edit my generic.xaml from Resources. But can't add news stated in Blend, for that I must go to xaml.