Bigsby

I created the following class:

C#

namespace MyNameSpace

{

public class MyTextBlock : System.Windows.Controls.TextBlock

{

}

}

And then I tried to set a style for it:

XAML

<Style TargetType="{x:Type MyNameSpace:MyTextBlock}">

<Setter Property="Foreground" Value="Yellow"/>

</Style>

The Properties I set in the Style are not assumed in the rendering.

Any have an idea why this happens




Re: Windows Presentation Foundation (WPF) Class MyTExtBlock : TextkBlock & Style

Tim Dawson

You need to override the metadata for the DefaultStyleKey property. In your static constructor for the MyTextBlock type, override the metadata for the DefaultStyleKeyProperty field, passing typeof(MyTextBlock) as the new default value.




Re: Windows Presentation Foundation (WPF) Class MyTExtBlock : TextkBlock & Style

Bigsby

Testing.






Re: Windows Presentation Foundation (WPF) Class MyTExtBlock : TextkBlock & Style

Bigsby

Could you please help with the syntax

I'm not getting this thing right.

Where and how should I put it






Re: Windows Presentation Foundation (WPF) Class MyTExtBlock : TextkBlock & Style

Bigsby

Haven't got it yet.

Gone this far:

In C#:

namespace MyNameSpace

{

public class MyTextBlock : System.Windows.Controls.TextBlock

{

static MyTextBlock()

{

DefaultStyleKeyProperty.OverrideMetadata(typeof(MyTextBlock),new FrameworkPropertyMetadata(typeof (MyTextBlock)));

}

}

}

In Resources\Styles.xaml ResourceDictionary (with build action Resource):

<ResourceDictionary (...) xmlns:MyNameSpace="clr-namespace:MyNameSpace;assembly=MyNameSpace">

<Style TargetType="{x:Type MyNameSpace:MyTextblock}">

<Setter Property="FontSize" Value="8"/>

<Setter Property="Foreground" Value="#FFF0F010"/>

</Style>

</ResourceDictionary>

In Page1.xaml

<Page (...) xmlns:MyNameSpace="clr-namespace:MyNameSpace;assembly=MyNameSpace">

<Page.Resources>

<ResourceDictionary>

<ResourceDictionary.MergedDictionaries>

<ResourceDictionary x:Name="Styles" Source="Resources\Styles.xaml"/>

</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

</Page.Resources>

<Grid Background="#FF000040">

<MyNameSpace:MyTextBlock Text="Any text..."/>

</Grid>

</Page>

What am I doing wrong now






Re: Windows Presentation Foundation (WPF) Class MyTExtBlock : TextkBlock & Style

Bigsby

Never finished this subject but what I did, at the time, was to create for both TextBox and TextBlock separate Templates that started of with a Border that used the same style...

Later I moved in a different direction so I using only TextBox and, in same case, some are IsReadOnly="True".