Toows
Toows

Reputation: 93

Change foreground of a textblock in a ToggleButton

I'm new to WPF and I'm totaly stuck with a foreground who don't want to update :@

I wan't to make a radiobutton with a togglebutton behavior (that's ok), but i want to wrap the text inside this button.

If the content is defined in the toggleButton, my foreground become white. But if i use a Textblock inside the togglebutton (with a wrap), my foreground don't want to change.

My two buttons in XAML

        <RadioButton GroupName="line1" Grid.Row="1" Grid.Column="1" Style="{DynamicResource ToggleButtonStyle1}">
        <RadioButton.Content>
            <TextBlock TextWrapping="Wrap" >I can wrap but ...</TextBlock>
        </RadioButton.Content>
    </RadioButton>

    <RadioButton GroupName="line0" Grid.Row="1" Grid.Column="2" Style="{DynamicResource ToggleButtonStyle1}" Content="i can't Wrap and it's not good"/>

And my ControlTemplate in app.xaml

 <ControlTemplate TargetType="{x:Type ToggleButton}">
    <Border x:Name="border" BorderThickness="1" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
        <Border.BorderBrush>
            <SolidColorBrush Color="{DynamicResource BleuClair}"/>
        </Border.BorderBrush>
        <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="Button.IsDefaulted" Value="true">
            <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Static.Background}"/>
            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Static.Border}"/>
            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        </Trigger>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="White"/>
        </Trigger>
        <Trigger Property="IsPressed" Value="true">
            <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="White"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="White"/>
        </Trigger>
        <Trigger Property="IsChecked"  Value="true">
            <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="White"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

I'm not sure if i need to update the ControlTemplate or the ContentPresenter to add "Something" about the textblock or if i need to update the XAML to make a binding to this custom style.

Thank you for your time :)

Upvotes: 0

Views: 1639

Answers (2)

Toows
Toows

Reputation: 93

I find (finally :'( ) my solution. I think it can be done in the ControlTemplate, but i don't know how ^^

I had this :

<RadioButton GroupName="line1" Grid.Row="1" Grid.Column="1" Style"{DynamicResource ToggleButtonStyle1}">
        <TextBlock TextWrapping="Wrap" >I can wrap but ...</TextBlock>
</RadioButton>

And to apply my foreground property (from the radioButton), i simply add a binding to the contentPresenter.

    <RadioButton GroupName="line1" Grid.Row="1" Grid.Column="1" Style="{DynamicResource ToggleButtonStyle1}">
        <TextBlock Foreground="{Binding Path=(TextElement.Foreground), RelativeSource={RelativeSource AncestorType=ContentPresenter}}"
                   TextWrapping="Wrap" >I can wrap but ...</TextBlock>
    </RadioButton>

Upvotes: 1

Matt L.
Matt L.

Reputation: 768

It sounds like what you're looking for is an event that will toggle the foreground color when clicked. Try setting up your XAML like this:

<RadioButton Click="RadioButton_Clicked" GroupName="line1" Grid.Row="1" Grid.Column="1" Style="{DynamicResource ToggleButtonStyle1}">
     <TextBlock TextWrapping="Wrap" >Text here</TextBlock>
</RadioButton>

Now in your code, handle the event with a method

private void RadioButton_Clicked(object sender, RoutedEventArgs e)
{
    // get an object of the button
    RadioButton button = (RadioButton)sender;

    // access the textblock to change the foreground
    TextBlock text = (TextBlock)button.Child;

    // change the foreground to white
    text.Foreground = Brushes.White;
}

Hopefully this helps or is close to what you are looking for!

Upvotes: 0

Related Questions