【解決方法】色が適用されない理由


色が適用されない理由がわかりません

C#
<StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                <RadioButton Content="Use Login OTP" Style="{StaticResource {x:Type ToggleButton}}" Padding="12,8,12,8" GroupName="View"/>
                <RadioButton Content="Use Login Code" Style="{StaticResource {x:Type ToggleButton}}" Padding="12,8,12,8" GroupName="View"/>
                <RadioButton Content="Use Secret Pin" Style="{StaticResource {x:Type ToggleButton}}" Padding="12,8,12,8" GroupName="View"/>
            </StackPanel>

C#
<SolidColorBrush x:Key="StashOTPOptionFontBrush" Color="#DDDDDD"/>

<LinearGradientBrush x:Key="StashOTPOptionBGBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientStop Offset="0" Color="#888888"/>
    <GradientStop Offset="1" Color="#222222"/>
</LinearGradientBrush>

<SolidColorBrush x:Key="StashOTPOptionBorderBrush" Color="#333333"/>
<LinearGradientBrush x:Key="CheckedBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientStop Offset="0" Color="#555555"/>
    <GradientStop Offset="1" Color="#111111"/>
</LinearGradientBrush>

<ControlTemplate x:Key="ToggleButtonLeft" TargetType="{x:Type ToggleButton}">
    <Border Name="Border" Background="{StaticResource StashOTPOptionBGBrush}" BorderBrush="{StaticResource StashOTPOptionBorderBrush}" BorderThickness="1" CornerRadius="5,0,0,5">
        <ContentPresenter HorizontalAlignment="Center" Margin="{TemplateBinding Padding}" VerticalAlignment="Center" Content="{TemplateBinding Content}" TextBlock.FontWeight="Bold" TextBlock.Foreground="{StaticResource StashOTPOptionFontBrush}"/>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="ToggleButton.IsMouseOver" Value="true">
            <Setter TargetName="Border" Property="Background" Value="#808080"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="true">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource CheckedBrush}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="ToggleButtonMid" TargetType="{x:Type ToggleButton}">
    <Border Name="Border" Background="{StaticResource StashOTPOptionBGBrush}" BorderBrush="{StaticResource StashOTPOptionBorderBrush}" BorderThickness="0,1,0,1" CornerRadius="0" />
    <ControlTemplate.Triggers>
        <Trigger Property="ToggleButton.IsMouseOver" Value="true">
            <Setter TargetName="Border" Property="Background" Value="#808080"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="true">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource CheckedBrush}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="ToggleButtonRight" TargetType="{x:Type ToggleButton}">
    <Border Name="Border" Background="{StaticResource StashOTPOptionBGBrush}" BorderBrush="{StaticResource StashOTPOptionBorderBrush}" BorderThickness="1" CornerRadius="0, 5, 5, 0" >
        <ContentPresenter HorizontalAlignment="Center" Margin="{TemplateBinding Padding}" VerticalAlignment="Center" Content="{TemplateBinding Content}" TextBlock.FontWeight="Bold" TextBlock.Foreground="{StaticResource StashOTPOptionFontBrush}"/>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="ToggleButton.IsMouseOver" Value="true">
            <Setter TargetName="Border" Property="Background" Value="#808080"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="true">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource CheckedBrush}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

私が試したこと:

色が要素に直接適用された場合にのみ機能します

解決策 1

ブラシの簡単なテストを行いました:

XML
<Window.Resources>
    <LinearGradientBrush x:Key="StashOTPOptionBGBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Offset="0" Color="#888888"/>
        <GradientStop Offset="1" Color="#222222"/>
    </LinearGradientBrush>
</Window.Resources>

<Grid Background="{StaticResource StashOTPOptionBGBrush}">

</Grid>

正常に動作します。

今のために ControlTemplate スタイルとして:

XML
<Window.Resources>
    <LinearGradientBrush x:Key="StashOTPOptionBGBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Offset="0" Color="#888888"/>
        <GradientStop Offset="1" Color="#222222"/>
    </LinearGradientBrush>
    <Style x:Key="MyRadioButtonStyle" TargetType="RadioButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="RadioButton">
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <Grid>
                            <Ellipse Width="16" Height="16" Fill="{TemplateBinding Background}"
                                     Stroke="{TemplateBinding BorderBrush}"
                                     StrokeThickness="{TemplateBinding BorderThickness}"/>
                            <Ellipse x:Name="Checked" Width="10" Height="10" Fill="Black" Visibility="Collapsed"/>
                        </Grid>
                        <Label Margin="5 0 0 0" Content="{TemplateBinding Content}"
                               Foreground="White"
                               Background="{StaticResource StashOTPOptionBGBrush}"/>
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter TargetName="Checked" Property="Visibility" Value="Visible"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

次に適用します。

XML
<Grid>
    <RadioButton HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 Content="Check if wanted"
                 Style="{StaticResource MyRadioButtonStyle}"/>
</Grid>

コメント

タイトルとURLをコピーしました