Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Creating a sub-class library of common controls?
Message
From
02/07/2008 15:50:34
 
 
To
02/07/2008 13:43:15
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Miscellaneous
Thread ID:
01328319
Message ID:
01328401
Views:
13
A XAML file can only have one root element and you can only do this x:Class="wpf2.FieldTextBox" in that single root element. So what your proposing won't work, they need to be separate files.

Subclassing is a bit different in WPF than is was in earlier languages.

Here's my view on it:

If it's for appearance use styles and templates in your resource dictionaries, do not use subclasses just to restyle elements in WPF. Only subclass a base class other than a user control or a window when you need to add codebehind functionality to it that you cannot easily add in XAML or with Dependency Properties.

The more subclasses you add the more complex your XAML resource dictionaries become, so it's a good idea to keep it as simple as you can. Remember that something like a red or blue foreground in your sample below (i realize that was just a sample) is often based on a property of the item behind displayed. Handle that with a trigger rather than creating a different style.

That said I do have subclasses, For instance I have 2 for text boxes, 1 that does VFP like text entry handling and one that does auto complete. I am however strongly considering combining them into 1 subclass as some point.

Most (but not all) of my reusable subclasses do not have a XAML component to them. They are just a C# file. If I want to restyle them I do it in a style in the Resource Dictionaries.

I load my resources at the app level but you could also do them at the window level.
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Themes/Default.xaml" />
        <ResourceDictionary Source="/cdsWPFControls;component/Drawings/Contacts.xaml" />
        <ResourceDictionary Source="/cdsWPFControls;component/Drawings/Tools.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>
HTH

John


>I want to subclass certain base controls before using them on my forms so I can have one central place to adjust the visual appreance of them.
>
>I have this working in a file called MyControls.xaml, but I cannot figure out how to add a second class in this file.
>
>Here it is with just one sub-classed control, a TextBlock:
>
>
><TextBlock x:Class="wpf2.FieldLabel"
>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>    Foreground="Red">
></TextBlock>
>
>
>
>
>But, when I add a second class into the file (for a TextBox), it gives me an error, saying 'There are multiple root elements. Line 9, position 6.' XML is not valid.
>
>
><TextBlock x:Class="wpf2.FieldLabel"
>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>    Foreground="Red">
></TextBlock>
>
>
><TextBox x:Class="wpf2.FieldTextBox"
>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>   Foreground="Blue">
></TextBox>
>
>
>
>So, how can I have multiple sub-classes in a file? Do I have to create one file for each sub-classed control?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform