Codejock Forums Homepage
Forum Home Forum Home > General > XAML Snippets
  New Posts New Posts RSS Feed - GENERAL: XAML Markup TextBlock.Style as attribute
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

GENERAL: XAML Markup TextBlock.Style as attribute

 Post Reply Post Reply
Author
Message
mnowaczy View Drop Down
Groupie
Groupie


Joined: 16 June 2008
Status: Offline
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote mnowaczy Quote  Post ReplyReply Direct Link To This Post Topic: GENERAL: XAML Markup TextBlock.Style as attribute
    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.
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
mnowaczy View Drop Down
Groupie
Groupie


Joined: 16 June 2008
Status: Offline
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote mnowaczy Quote  Post ReplyReply Direct Link To This Post 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>
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 17 June 2008 at 6:31am
yeap. also right :)
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.102 seconds.