Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Can you apply a style to an element that you've defined?

 
 
Old 02-02-2006   #1 (permalink)
Jason Dolinger


 
 

Can you apply a style to an element that you've defined?

Is this possible? I've created my own element which basically extends
from Border and contains a TextBlock, called DataGridHeader:

<Border x:Name="colHeader" x:Class="Lab49.Controls.DataGridHeader"
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
>

<TextBlock
x:Name="headerText"
TextAlignment="Center"
/>
</Border>


In a .xaml file that is using this class, I'd then like to apply a style
to it (to set properties such as Background, Margin, BorderBrush and
BorderThickness.)

The style looks something like this:


<?Mapping XmlNamespace="lab49control" ClrNamespace="Lab49.Controls" ?>
<ItemsControl x:Class="Lab49.Controls.DataGrid"
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
xmlns:control="lab49control"
Loaded="OnLoad"
BorderBrush="Black"
BorderThickness="1"
>


<ItemsControl.Resources>

<Style TargetType="{controlataGridHeader}">
<!-- A bunch of property setters ... -->
</Style>
</ItemsControl.Resources>

.... class layout

</ItemsControl>

This always seems to result in the following build error:

Error 19 The value '{controlataGridHeader}' is not a valid
MarkupExtension expression. Could not resolve 'DataGridHeader' in
namespace 'lab49control'. 'DataGridHeader' must be a subclass of
MarkupExtension. Line 14 Position 12. C:\Documents and
Settings\jdolinger\My Documents\Visual Studio\Projects\WPF
Demos\TradingDemo\DataGrid Control\DataGrid.xaml 14 12 DataGrid Control

I see two possibilities:
1. This error is actually complaining about the way I'm using the style
because it's a user defined class, not an existing control, and WPF
won't support that right now.
2. There is some problem resolving the class DataGridHeader, there must
be something wrong with the way I've set up the XmlNamespace and
ClrNamespace mapping and qualified the control name with "control:..."

Thanks, does any have any ideas about this one? It's always greatly
appreciated!

Jason





My System SpecsSystem Spec
Old 02-02-2006   #2 (permalink)
Drew Marsh


 
 

Re: Can you apply a style to an element that you've defined?

Jason Dolinger wrote:

> This always seems to result in the following build error:
>
> Error 19 The value '{controlataGridHeader}' is not a valid
> MarkupExtension expression. Could not resolve 'DataGridHeader' in
> namespace 'lab49control'. 'DataGridHeader' must be a subclass of
> MarkupExtension. Line 14 Position 12. C:\Documents and
> Settings\jdolinger\My Documents\Visual Studio\Projects\WPF
> Demos\TradingDemo\DataGrid Control\DataGrid.xaml 14 12 DataGrid
> Control
>
> I see two possibilities:
> 1. This error is actually complaining about the way I'm using the
> style
> because it's a user defined class, not an existing control, and WPF
> won't support that right now.
> 2. There is some problem resolving the class DataGridHeader, there
> must
> be something wrong with the way I've set up the XmlNamespace and
> ClrNamespace mapping and qualified the control name with "control:..."
> Thanks, does any have any ideas about this one? It's always greatly
> appreciated!


Try this:

<Style TargetType="{x:Type controlataGridHeader}">

Everything else you're trying to do should work, though I'm not sure why
you're going through the trouble of subclassing when you can just use templates.
Unless you're adding true behavior that requires code, it's probably not
the right approach.

HTH,
Drew

___________________________________
Drew Marsh
Chief Software Architect
Mimeo.com, Inc. - http://www.mimeo.com
Microsoft C# / WPF MVP
Weblog - http://blog.hackedbrain.com/


My System SpecsSystem Spec
Old 02-04-2006   #3 (permalink)
Jason Dolinger


 
 

Re: Can you apply a style to an element that you've defined?

Drew Marsh wrote:

>
> Everything else you're trying to do should work, though I'm not sure why
> you're going through the trouble of subclassing when you can just use
> templates. Unless you're adding true behavior that requires code, it's
> probably not the right approach.
>
> HTH,
> Drew
>


Drew, thanks as always. What kind of template should I use here, a
ControlTemplate which contains both the Border and TextBlock objects?
That would make sense, but I can't apply it to a TextBlock (which
doesn't extend from control).

Jason
My System SpecsSystem Spec
Old 02-04-2006   #4 (permalink)
Jason Dolinger


 
 

Re: Can you apply a style to an element that you've defined?

Drew Marsh wrote:
> Jason Dolinger wrote:


>
> Everything else you're trying to do should work, though I'm not sure why
> you're going through the trouble of subclassing when you can just use
> templates. Unless you're adding true behavior that requires code, it's
> probably not the right approach.
>
> HTH,
> Drew


Drew, I'm starting to get the idea. I created a control template which
will restyle my button, giving it a Border and a TextBlock:

<ControlTemplate x:Key="GridHeaderTemplate">
<Border Background="LightGray" BorderBrush="White"
BorderThickness="1, 1, 1, 1">
<TextBlock Text="???" HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
</ControlTemplate>

Then in code where I'm creating my GridHeader buttons, I assign the
template like so:

Button colhead = new Button();
colhead.Template = (ControlTemplate)FindResource("GridHeaderTemplate");

However, there is one trick here, which you can see in ??? above in my
ControlTemplate. How exactly am I supposed to bind the Text property of
the TextBlock to my Button's Text content? In the XAML layer, the
ControlTemplate has no idea about Button (or whatever) that it's being
applied to.

I'm trying hard to reorganize my thinking from objects/inheritance/etc.
to controls, styles, and templates but I keep running up against
roadblocks like these.

Thanks,
Jason
My System SpecsSystem Spec
Old 02-04-2006   #5 (permalink)
Drew Marsh


 
 

Re: Can you apply a style to an element that you've defined?

Jason Dolinger wrote:

> However, there is one trick here, which you can see in ??? above in my
> ControlTemplate. How exactly am I supposed to bind the Text property
> of the TextBlock to my Button's Text content? In the XAML layer, the
> ControlTemplate has no idea about Button (or whatever) that it's being
> applied to.


Ok, so now you just need to look into TemplateBinding[1]. It would go a little
something like this:

<TextBlock Text="{TemplateBinding Text}" HorizontalAlignment="Center"
VerticalAlignment="Center" />

HTH,
Drew

[1] http://windowssdk.msdn.microsoft.com...gExtension.asp
___________________________________
Drew Marsh
Chief Software Architect
Mimeo.com, Inc. - http://www.mimeo.com
Microsoft C# / WPF MVP
Weblog - http://blog.hackedbrain.com/


My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
nothing highly defined in HD Graphic cards
Font Size - XP style vs Vista style Vista General
Font Size - XP style vs Vista style Vista General
Font Size - XP style vs Vista style Vista General
How can I tell if a function is already defined? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46