Setting the style's TargetType property to a common class

Is it possible to set the TargetType property of a style in XAML in a Generic Class?

public class Selector<T> : Control { } 

and then in xaml

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

This does not work because there is no type argument in Selector.

+4
source share
3 answers

You cannot bind to a public generic type of type List<T> , however you can bind to a private generic type of type List<Person> by specifying a placeholder type.

C # :

 class People : List<Person> {} 

Xaml

 <Style TargetType="{x:Type People}"> ... </Style> 

Update : you need to specify TargetType or the x:Key property for the style, not both.

+2
source

In XAML, general features have fairly limited support. However, Mike Hillberg has a rather interesting post here about custom markup extensions that can help.

+1
source

I understand that you are using regular WPF, not Silverlight? If I'm not mistaken, you can say the following:

 <Style TargetType="{x:Type Control}" x:Key="{x:Type Control}"> </Style> 
0
source

Source: https://habr.com/ru/post/1286265/


All Articles