Print Page | Close Window

GENERAL: XAML Markup TextBlock.Style as attribute

Printed From: Codejock Forums
Category: General
Forum Name: XAML Snippets
Forum Description: Post your XAML snippets here for everyone to enjoy :)
URL: http://forum.codejock.com/forum_posts.asp?TID=11053
Printed Date: 06 May 2024 at 6:25pm
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: GENERAL: XAML Markup TextBlock.Style as attribute
Posted By: mnowaczy
Subject: GENERAL: XAML Markup TextBlock.Style as attribute
Date Posted: 16 June 2008 at 9:08am
I have style defined im my Page resources like this:
<Style x:Key="Txt_1" TargetType="TextBlock">
   <Setter Property="Padding" Value="0,5,0,5"/>
</Style>

then I use it for elements in stack panel and it works well:
<StackPanel>
    <TextBlock Style="{StaticResource Txt_1}">sample 1</TextBlock>
    <TextBlock Style="{StaticResource Txt_1}">sample 2</TextBlock>
</StackPanel>

As long as I want all TextBlock inside StackPanel to be the same style I tried this:
<StackPanel TextBlock.Style="{StaticResource Txt_1}">
    <TextBlock>sample 1</TextBlock>
    <TextBlock>sample 2</TextBlock>
</StackPanel>

This approach works well with for example "FontWeight" property but not with "Style". I use your MarkupPad to test it.



Replies:
Posted By: Oleg
Date Posted: 17 June 2008 at 12:28am
Hi,
 
Some properties in XAML are "Attached", some "Inherited". This can work for FontWeight - its Attached and Inherited property, but can't for Style property.
 
Here valid XAML for your task:
 
<Page xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>

<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBlock">
   <Setter Property="Padding" Value="0,5,0,5"/>
</Style>
</StackPanel.Resources>
    <TextBlock>sample 1</TextBlock>
    <TextBlock>sample 2</TextBlock>
</StackPanel>
</Page>


-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: mnowaczy
Date Posted: 17 June 2008 at 5:27am
Thanks for explanation.

As far as this style is global for page I don't want to define it many times in different panels. I made such workaround :)

<StackPanel>
<StackPanel.Resources>
   <Style TargetType="TextBlock" BasedOn="{StaticResource Txt_1}"/>
</StackPanel.Resources>
   <TextBlock>sample 1</TextBlock>
   <TextBlock>sample 2</TextBlock>
</StackPanel>


Posted By: Oleg
Date Posted: 17 June 2008 at 6:31am
yeap. also right :)

-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net