locked
chang textblock Foreground property after defining Foreground in textblock style in Resource Directory RRS feed

  • Question

  • Hi
      
       I have define Style for textblock in Resource Directory & define Foreground property as Black . Now I have define another style of button in which I have define template of Button using textblock ,here I have define a different foreground in this template .But the problem is ,it is not taking this foreground ,so button shows the foreground as Black But it would be shown in blue .Pls Suggest me Solution.........

       <Style  TargetType="{x:Type TextBlock}" BasedOn="{x:Null}">
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="FontSize" Value="12"/>
    </Style>

       <Style x:Key="AttachLinkButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <!--<Setter Property="OverridesDefaultStyle" Value="false" />-->
            <!--<Setter Property="Foreground" Value="White"/>-->
            <Setter Property="Width" Value="Auto"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <TextBlock x:Name="tb" Background="Yellow" Foreground="Blue" Cursor="Hand" TextDecorations="Underline" TextWrapping="Wrap" >
                                  <ContentPresenter VerticalAlignment="Center" RecognizesAccessKey="true"/>
                            </TextBlock>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="true" >
                                <Setter Property="Cursor" Value="Hand" />
                                <Setter TargetName="tb" Property="BitmapEffect">
                                    <Setter.Value>
                                        <DropShadowBitmapEffect ShadowDepth="1" Direction="330" Color="Black" Opacity="0.5" Softness="0.25" />
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    Thursday, June 18, 2009 11:51 AM

All replies

  • Your XAML works correctly. The only thing you might be forgetting is that you must specify the Style attribute on your button when you create an instance of a button like so:
    <Button Height="25" Width="100" Style="{StaticResource AttachLinkButtonStyle}" Content="Hello" />

    You must do this since you gave your button style a x:Key. NOTE: You did not give your TextBlock style a key so by default all TextBlock instances will use that style throughout your entire application.

    If you run the following XAML in XAMLPad (Free download from microsoft) it displays like you said it should:

     <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
        <StackPanel>
        <StackPanel.Resources>
           <Style  TargetType="{x:Type TextBlock}" BasedOn="{x:Null}">
    	       <Setter Property="Foreground" Value="Black"/>
           	<Setter Property="FontSize" Value="12"/>
    	</Style>
    
       <Style x:Key="AttachLinkButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <!--<Setter Property="OverridesDefaultStyle" Value="false" />-->
            <!--<Setter Property="Foreground" Value="White"/>-->
            <Setter Property="Width" Value="Auto"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <TextBlock x:Name="tb" Background="Yellow" Foreground="Blue" Cursor="Hand" TextDecorations="Underline" TextWrapping="Wrap" >
                                  <ContentPresenter VerticalAlignment="Center" RecognizesAccessKey="true"/>
                            </TextBlock>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="true" >
                                <Setter Property="Cursor" Value="Hand" />
                                <Setter TargetName="tb" Property="BitmapEffect">
                                    <Setter.Value>
                                        <DropShadowBitmapEffect ShadowDepth="1" Direction="330" Color="Black" Opacity="0.5" Softness="0.25" />
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        </StackPanel.Resources>
            <Button Height="25" Width="100" Style="{StaticResource AttachLinkButtonStyle}" Content="Hello" />
    	 <TextBlock Text="Test Color" Width="100" Height="25" />
        </StackPanel>
    </Page>

    Billy Jacobs
    Thursday, June 18, 2009 4:16 PM
  • Thanks for Replying.......
    but dear, its not complete solution for me...

    Actually, I am creating a button dynamically and assigning a style to it as follows, But still it's foreground shows in black rather than in blue.Another thing is ,it shows Underline in blue but fonts in black.Why this happens please tell me the solution....


                    Button lnkButtonFileName = new Button();
                    lnkButtonFileName.Content = E.Element("FileName").Value.Split(',')[2].ToString();
                    lnkButtonFileName.CommandParameter = E.Element("FileName").Value.ToString();
                    ResourceDictionary rd = new ResourceDictionary();
                    rd.Source = new Uri(@"Styles/Theme_Golden.xaml", UriKind.Relative);

                    lnkButtonFileName.Style = (Style)rd["AttachLinkButtonStyle"];


    Actually I thought one solution that remove the foreground from the textblock then It creates another very big problem ,then My treeviewitem, CheckBox foreground All are showing in white color . So I have to define  strictly My Textblock foreground in Black. But Why this happens I want to know.....Please answer me this.


    Friday, June 19, 2009 7:07 AM
  • Hi,

    Could you provide a simplified and ready-to-run sample to demonstrate the issue? so as to we can have a clear idea of where you are having issues.

    Thanks.


    Jim Zhou -MSFT
    Wednesday, June 24, 2009 1:21 PM