locked
When I change the defaul style of textblock, the default of style of button(the textblock part of button) changes too. RRS feed

  • Question

  • Hi, I override the default textblock style for all textblock in the app.xaml file.

    Button I found that all the textblocks of buttons are changed into the same style.

    So, the UI seems very strange.

    I know that a button contains a textblock which contains the text.

    But, is there any way to solve this problem. I just want to change the textblock only, not all of the textblocks that contained in other controls.

    Thank you. Any suggestion in appreciated.


    • Edited by Ax Max Tuesday, July 1, 2014 9:01 AM
    Tuesday, July 1, 2014 9:00 AM

Answers

  • No, as you have already discovered, if you define a style without an x:Key attribute and with the TargetType set to TextBlock in your App.xaml, it will apply to ALL TextBlock elements in your application including TextBlock elements that reside in other control's templates.

    You should set an x:Key for the style and reference if from all TextBlocks that you want to use this style using the StaticResource or DynamicResource markup extension, or you could define your own custom TextBlock element and style this one implicitly:

    namespace WpfApplication6
    {
     public class MyTextBlock : System.Windows.Controls.TextBlock
     {
     }
    }

    <Application x:Class="WpfApplication6.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication6"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
             <Style TargetType="{x:Type local:MyTextBlock}">
       <Setter Property="Foreground" Value="Green"/>
       </Style>
        </Application.Resources>
    </Application>

    <local:MyTextBlock Text="text..."></local:MyTextBlock>


    Tuesday, July 1, 2014 10:58 AM