英文:
How to change the color of Text of ContentPresenter in .Net MAUI
问题 {#heading}
我在我的应用程序中使用了自定义样式的单选按钮,其中使用ContentPresenter来显示文本/内容。我想要更改文本的颜色。
我尝试过TextColor 和TextBlock.Foreground,但它们没有解决这个问题。
这是我的代码示例:
<ControlTemplate x:Key="RadioButtonTemplate">
    <Border Stroke="Transparent" BackgroundColor="Transparent">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroupList>
                <VisualStateGroup x:Name="CheckedStates">
                    <VisualState x:Name="Checked">
                        <VisualState.Setters>
                            <Setter TargetName="check" Property="Opacity" Value="1" />
                            <Setter TargetName="check_circle" Property="Opacity" Value="1" />
                            <Setter TargetName="border_circle" Property="Opacity" Value="0" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="Unchecked">
                        <VisualState.Setters>
                            <Setter TargetName="check" Property="Opacity" Value="0" />
                            <Setter TargetName="check_circle" Property="Opacity" Value="0" />
                            <Setter TargetName="border_circle" Property="Opacity" Value="1" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </VisualStateManager.VisualStateGroups>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="20" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid WidthRequest="20" HeightRequest="20" Grid.Column="0" VerticalOptions="Center" HorizontalOptions="Center">
                <Ellipse x:Name="border_circle" StrokeThickness="2" Stroke="{StaticResource PlaceholderColor}" Fill="Transparent" WidthRequest="18" HeightRequest="18" HorizontalOptions="Center" VerticalOptions="Center" />
                <Ellipse x:Name="check" Fill="{StaticResource BackgroundColor}" WidthRequest="10" HeightRequest="10" HorizontalOptions="Center" VerticalOptions="Center" />
                <Ellipse x:Name="check_circle" StrokeThickness="2" Stroke="{StaticResource BackgroundColor}" WidthRequest="18" HeightRequest="18" HorizontalOptions="Center" VerticalOptions="Center" />
            </Grid>
            <ContentPresenter Margin="10,0,0,0" Grid.Column="1" HorizontalOptions="Start" VerticalOptions="Center" />
        </Grid>
    </Border>
</ControlTemplate>
<Style TargetType="RadioButton" x:Key="RadioButtonStyle">
    <Setter Property="ControlTemplate" Value="{StaticResource RadioButtonTemplate}" />
    <Setter Property="TextColor" Value="{StaticResource Black}" />
</Style>
英文:
I have using custom styled Radio Button in my application where it uses ContentPresenter for displaying text/content. I want to change the color of the Text.
I have tried TextColor and TextBlock.Foreground but these doesnot solved it.
This is the Sample of my code:
<ControlTemplate x:Key="RadioButtonTemplate">
<Border Stroke="Transparent" BackgroundColor="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroupList>
<VisualStateGroup x:Name="CheckedStates">
<VisualState x:Name="Checked">
<VisualState.Setters>
<Setter TargetName="check" Property="Opacity" Value="1" />
<Setter TargetName="check_circle" Property="Opacity" Value="1" />
<Setter TargetName="border_circle" Property="Opacity" Value="0" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unchecked">
<VisualState.Setters>
<Setter TargetName="check" Property="Opacity" Value="0" />
<Setter TargetName="check_circle" Property="Opacity" Value="0" />
<Setter TargetName="border_circle" Property="Opacity" Value="1" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</VisualStateManager.VisualStateGroups>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid WidthRequest="20" HeightRequest="20" Grid.Column="0" VerticalOptions="Center" HorizontalOptions="Center">
<Ellipse x:Name="border_circle" StrokeThickness="2" Stroke="{StaticResource PlaceholderColor}" Fill="Transparent" WidthRequest="18" HeightRequest="18" HorizontalOptions="Center" VerticalOptions="Center" />
<Ellipse x:Name="check" Fill="{StaticResource BackgroundColor}" WidthRequest="10" HeightRequest="10" HorizontalOptions="Center" VerticalOptions="Center" />
<Ellipse x:Name="check_circle" StrokeThickness="2" Stroke="{StaticResource BackgroundColor}" WidthRequest="18" HeightRequest="18" HorizontalOptions="Center" VerticalOptions="Center" />
</Grid>
<ContentPresenter Margin="10,0,0,0" Grid.Column="1" HorizontalOptions="Start" VerticalOptions="Center" />
</Grid>
</Border>
</ControlTemplate>
<Style TargetType="RadioButton" x:Key="RadioButtonStyle">
<Setter Property="ControlTemplate" Value="{StaticResource RadioButtonTemplate}" />
<Setter Property="TextColor" Value="{StaticResource Black}" />
</Style> 
答案1 {#1}
得分: 0
你可以在使用ControlTemplate时,设置RadioButton.Content中的TextColor。
<RadioButton Value="Elephant">
    <RadioButton.Content>
        <StackLayout>
            <Image Source="dotnet_bot.png"
                   HorizontalOptions="Center"
                   VerticalOptions="Center" />
            <Label Text="Elephant" TextColor="Yellow"
                   HorizontalOptions="Center"
                   VerticalOptions="End" />
        </StackLayout>
    </RadioButton.Content>
</RadioButton>
更多信息,请参考Redefine RadioButton appearance。
希望对你有帮助! 英文:
You could set the TextColor in RadioButton.Content when consuming the ControlTemplate
<RadioButton Value="Elephant">
<RadioButton.Content>
<StackLayout>
<Image Source="dotnet_bot.png"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Label Text="Elephant" TextColor="Yellow"
HorizontalOptions="Center"
VerticalOptions="End" />
</StackLayout>
</RadioButton.Content>
</RadioButton>
For more info, you could refer to Redefine RadioButton appearance
Hope it helps!
 51工具盒子
51工具盒子 
                 
                             
                         
                         
                         
                        