首页 文章

在ToggleButton中更改文本块的前景

提问于
浏览
0

我是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以添加关于文本块的“Something”,或者我是否需要更新XAML以对此自定义样式进行绑定 .

感谢您的时间 :)

2 回答

  • 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;
    }
    

    希望这有助于或接近您正在寻找的东西!

  • 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>
    

相关问题