在ToggleButton中更改文本块的前景

时间:2017-07-06 14:05:08

标签: wpf xaml

我是WPF的新手,我总是坚持一个不想更新的前景:@

我不想制作一个带有togglebutton行为的单选按钮(这没关系),但我想将文本包装在这个按钮内。

如果内容是在toggleButton中定义的,那么我的前景会变为白色。但是如果我在togglebutton中使用Textblock(带有换行),我的前景不想改变。

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"/>

我的ControlTemplate在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>

我不确定是否需要更新ControlTemplate或ContentPresenter来添加&#34; Something&#34;关于文本块或者如果我需要更新XAML以对此自定义样式进行绑定。

感谢您的时间:)

2 个答案:

答案 0 :(得分:0)

听起来你正在寻找的是一个在点击时切换前景色的事件。尝试像这样设置你的XAML:

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

现在在您的代码中,使用方法

处理事件
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;
}

希望这有助于或接近您所寻找的目标!

答案 1 :(得分:0)

我发现(最后:'()我的解决方案。 我认为它可以在ControlTemplate中完成,但我不知道如何^^

我有这个:

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

要应用我的前景属性(来自radioButton),我只需添加一个绑定到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>