tags:

views:

649

answers:

3

Is it possible to set a style's TargetType property in XAML to a Generic Class?

public class Selector<T> : Control { }

and then in xaml

<Style x:TargetType="Selector">
<Setter Property="MyProperty" Value="Green" />
</Style>

This wont work because Selector is missing a type argument.

A: 

I take it you're using regular WPF, not Silverlight? If I'm not mistaken, you could say this:

<Style TargetType="{x:Type Control}" x:Key="{x:Type Control}">
</Style>
Yes its WPF, but I need to connect it to a generic class - I'll update the question to be more clear.
vanja.
+1  A: 

AFAIR you cannot bind to an open generic type like List, you can however bind to a closed generic type like List by defining a placeholder type like

a code / .cs file

class People : List<Person {}

XAML

Update: early in the morning for me.. you either need to EITHER specify TargetType OR the Key property for a Style not both.

<Style TargetType="{x:Type People}"> ... </Style>
Gishu
I have stumbled onto this solution for similar xaml+generics situations. I already have 10 closed types per generic, I was hoping to not need to duplicate the style information.
vanja.
yeah... afaik XAML doesn't directly grasp generics... yet.
Gishu
+1  A: 

Generics have pretty limited support in XAML. That being said, Mike Hillberg has a pretty interesting post here about custom markup extensions that may help.

micahtan