Inheriting from a UserControl abstract subclass - silverlight

I have a set of UserControls that need to have a few similar properties. Thus I have defined an abstract subclass of UserControl that defines these properties and updated the .xaml.cs and .g.cs files to inherit from this base class. All compiles well and runs well. Great! But.... .g.cs files are generated and will be regenerated, so how do I tell Blend or Visual Studio to keep inheriting from my base class rather than UserControl?

You need to change the XAML a bit to prefix the UserControl declaration with a namespace:
<local:MyBaseControl x:Class="MyNameSpace.MyControl"
xmlns:local="clr-namespace:MyNameSpace"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Content -->
</local:MyBaseControl>
Where MyNameSpace is your namespace (duh!), MyBaseControl is your base class and MyControl is your control that inherits from MyBaseControl. The x:Class part doesn't need to be in the same namespace, I've just kept it the same for the example.
More info here and here.

Related

WPF CustomControl can't inherit from TextBlock control

I'm making a WPF CustomControlLibrary with CustomControls which inherit from standard controls like Label, TextBox etc.
When i try to make another CustomControl which inherit from TextBlock, i get strange errors.
It seems to be that a CustomControl can't inherit from the TextBlock.
But why?
Thanks in advance!
I have just created custom control inherited from TextBlock:
using System.Windows.Controls;
namespace WpfApplication1
{
public class CustomTextBlock : TextBlock
{
}
}
and used it within the same project:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<Grid>
<local:CustomTextBlock Text="Hello" />
</Grid>
</Window>
So the anser is: you can inherit from TextBlock
however, in order t use it in xaml, you have to compile the project first. There may be other errors in your code that prevents the project to compile and therefore you may be getting also errors like
The type 'local:CustomTextBlock' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built
or simmilar error:
The name "CustomTextBlock" does not exist in the namespace "clr-namespace:WpfApplication1".
Once you fix the other errors, these will disappear as well.

Hide control within a WPF User Control Library

I've a project made from the "WPF User Control Library" Template in Visual Studio.
This project contains one main usercontrol plus additional Windows/Usercontrols.
How can I "hide" these additional Windows/Usercontrols, so that the user can only import the main usercontrol from the assembly (I wanted to put a screen-shot to illustrate my question but unfortunately, my "reputation" is too low!).
Thx All
Fred
Make those controls internal. If you have classic UserControls with XAML and codebehind you will need to add x:ClassModifier="internal" to the root element in the XAML:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyNameSpace.MyUserControl"
x:ClassModifier="internal">
<!-- bla -->
</UserControl>
Maybe the usage of attributes will solve your problem. There is one attribute "DesignTimeVisible" inside the ComponentModel namespace. If you put such an attribute right above your class implementation and set it to false, the corresponding control should not be visible in the designers toolbox.
I believe that x:ClassModifier="internal" will make the entire user control internal. This may not be desirable.
Instead if you add x:FieldModifier="private" to those controls within the user control that you don't wish to be accessible to the UserControl consumer, the generated C# will have those controls as private. Note the use of lower case which is correct for a C# field modifier.

User defined top level control in XAML

A normal UserControl looks like this in XAML:
<UserControl x:Class="mynamespace.foo" ...namespaces...>
<!-- content -->
</UserControl>
I'd like to be able to define my own top level object, along the lines of:
<MyControl x:Class="mynamespace.mycontrol" ...namespaces...>
<!-- content -->
</UserControl>
Where MyControl derives from a UserControl itself.
Of course the compiler complains about "MyControl" not being found. Is there a way around this?
The root tag is the base class. That's why the root of the default Window1 is Window. Using the menu option Add > UserContol... is in fact creating a sub-class for UserContol.
If you have some common elements and want a control base class you can use the base class as the root tag. You can't derive your class from any class that has a xaml defined visual tree, but your base class can derive from UserConrtol.
First define your base class:
public class MyControlBase : UserControl
{
// ...
}
Then create your specific child class:
(You can start with the automatically created UserControl1 and change it from there)
public partial class MyControl1 : MyControlBase
{
public MyControl1()
{
InitializeComponent();
}
}
Then change the Xaml side to look like this:
<MyNamespace:MyControlBase
x:Class="MyNamespace.MyControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyNamespace="clr-namespace:MyNamespace">
This is a great way to make custom controls derived from built in ones other that UserControl. It is typically recommended to just use basic UserConrtols if you can and make a custom control only if you have to.
good luck,
Define your namespace in the XAML and then use your control name as the tag:
<Window ...
xmlns:my="..." />
<my:mycontrol ... />
</Window>
No. The XAML is declaring what a MyControl visually is, just as the code-behind is defining what a MyControl behaviourally is. Defining the visuals of a MyControl in terms of a MyControl wouldn't really make sense: it's the equivalent of, in the code-behind, deriving MyControl from MyControl, which you obviously wouldn't do.
In addition, WPF doesn't let you derive one UserControl class from another e.g. if BobsControl is a UserControl then you can't write <local:BobsControl x:Class="MyNamespace.MyControl... /> either. I believe this is because UserControls have a visual appearance (content) baked into their XAML and the content of the derived class would have to replace the content of the base class, so the visual inheritance is generally not useful.
However, you can do it if the top-level element you're deriving from is a custom control. Custom controls are lookless (not defined in XAML). So you can create your own top-level element as a custom control, and then derive "user" controls from that. (If you do go down this route, you'll probably want to derive your custom control from ContentControl or apply ContentPropertyAttribute, so that your top-level element can easily contain other XAML.)

Overspecified Namespace in UserControl fails WPF Build

I have a very simple user control, and I'm trying to instantiate it in XAML. I find that when I go a bit overzealous with the namespacing, I run into problems with x:Name.
Here is my UserControl:
<UserControl x:Class="UserControlTest.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300">
<Grid>
<Label Name="Label1">Label</Label>
</Grid>
</UserControl>
Here is the code-behind for the UserControl:
Namespace UserControlTest
Partial Public Class UserControl1
End Class
End Namespace
Now, note that I have the root namespace of my VB.Net project set to "UserControlTest". Knowing that, have a look at my main window:
Here is my main window:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:control="clr-namespace:UserControlTest.UserControlTest"
Title="Window1" Height="300" Width="300">
<Grid>
<control:UserControl1 />
</Grid>
</Window>
See how the control alias needs to have "UserControlTest.UserControlTest"? That's because I have the root namespace of my project set to UserControlTest, and I have defined the namespace of the UserControl to be UserControlTest, also. If I don't use a namespace for the UserControl, I don't have any troubles.
However, because I have done this, the build fails should I try to apply an x:Name to the UserControl, as follows:
<control:UserControl1 x:Name="test"/>
That will fail the build, with this error:
Type 'UserControlTest.UserControlTest.UserControl1' is not defined.
Can anybody explain why? Do I need to avoid putting my UserControls into namespaces just so I can give them x:Name values? I'd like to manipulate my UserControls from code-behind, and without an x:Name, I'm up the creek. But I don't want to sacrifice namespace usage just to get it!
Thanks very much.
I had the same problem (after rebuilding the project, first it worked fine...). I put UserControl into separate namespace.
What is the namespace defined as in the code-behind of your user control?
If your project was called Foo and you had a folder called Controls inside that project, any new user control added to that folder would be given the namespace Foo.Controls.
Then in your XAML you can reference it like so:
xmlns:Controls="clr-namespace:Foo.Controls"
...
<Controls:UserControl1 x:Name="uc1"/>
It seems like you have a naming issue.
EDIT:
Here's how I'm doing it in a project of mine.
StatusBar.xaml.cs
namespace Client.Controls.UserControls
{
public partial class StatusBar : UserControl
{
...
}
}
StatusBar.xaml
<UserControl x:Class="Client.Controls.UserControls.StatusBar">
</UserControl>
Main.xaml.cs
using Client.Controls.UserControls;
namespace Client
{
public partial class Main : Window
{
...
}
}
Main.xaml
<Window x:Class="Client.Main"
xmlns:UserControls="clr-namespace:Client.Controls.UserControls">
<UserControls:StatusBar x:Name="mainStatusBar" />
</Window>
I encountered the same problem in a vb.net project, and despite trying the suggestions here and elsewhere, could not solve the issue. In fact, I was able to take the exact same code out of our project to a new project, and it worked just fine (as far as I could determine all the configuration in the new project was identical). In the solution provided by David, I notice he is using c# - I am wondering if this is some weirdness associated with vb.net.
In the end, the user control I needed to use was quite simple and I implemented it as a resource ControlTemplate to get around the issue. Sorry I don't have a better answer, I am really not happy wih my findings...

WPF code-behind for resources?

I'm working on a wpf application, and up until recently, I had a ResourceDictionary inside my main window's resources part of the xaml. The resource dictionary contained an DataTemplate that was used to style several listboxes in the window. The xaml for this datatemplate contained pointers to event handlers, eg:
<Button n:Name="btnClickMe" Content="Click Me!" LeftMouseButtonUp="btnClickMe_Click" />
I recently decided to split the content of the window up into separate user controls, and to move my ResourceDictionary into it's own file. But, of course, there isn't a code-behind file for a resource dictionary file. How can I wire this up, with things split up as I've described?
Thanks in advance!
You can add a code-behind to a ResourceDictionary; just make sure your class names are referenced correctly. For instance, in the ResourceDictionary if you were working with AppStyles.xaml the XAML file would have a class of:
x:Class="Client.App.Shell.themes.AppStyles"
In the code-behind, AppStyles.xaml.cs, you would make sure to have the class:
namespace Client.App.Shell.themes
{
public partial class AppStyles
...
You can add a new class and name it with the same name as your resource dictionary plus the .cs extension and Visual Studio will automatically set things up so it becomes the code behind file.
For example if you have a resource dictionary called Buttons.xaml, add a file called Buttons.xaml.cs.
You should consider using RoutedCommands, I am thinking.
there are many many resources online, here are a couple that might help you.
http://msdn.microsoft.com/en-us/library/ms752308.aspx
http://www.devx.com/DevX/Article/37893/0/page/1

Resources