所以我有這個自定義樣式的 WPF 應用程式(但只是顏色和間距等)。我的復選框在單擊它們時具有這種奇怪的行為,即 UI 中未表示 IsChecked 狀態。單擊 CheckBox 時,會發生單擊影片,但不會出現勾號。 這是它的樣子
這是一個來自 Window 內容的示例:
<CheckBox Grid.Row="3" x:Name="CheckboxBattAlerts" VerticalAlignment="Center" IsChecked="False" IsThreeState="False"
Content="{x:Static p:Resources.Option_BatteryAlerts}"
Checked="CheckboxBattAlerts_CheckedChanged" Unchecked="CheckboxBattAlerts_CheckedChanged" />
CheckChanged 句柄方法:
private void CheckboxBattAlerts_CheckedChanged(object sender, RoutedEventArgs e)
{
App.Current.BattAlertsEnabled = CheckboxBattAlerts.IsChecked ?? false;
}
IsChecked 正在發生變化。
也許這是因為樣式,但是在洗掉樣式時我看不到變化。(我注釋掉了下面的代碼)這是適用于 CheckBox 的 XAML 樣式:
<Application.Resources>
(...)
<Style TargetType="{x:Type Control}">
<Setter Property="Foreground" Value="{StaticResource TextBlockBrush}" />
<Setter Property="Margin" Value="6,4" />
<Setter Property="FontSize" Value="10pt" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontFamily" Value="Bahnschrift SemiLight" />
</Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Margin" Value="4,2"/>
</Style>
</Application.Resources>
此外,當我添加Background="White" Foreground="Red"到任何 CheckBox 時,沒有任何變化。
我四處搜尋,但我能找到的“解決方案”根本沒有幫助。
那么,我錯過了什么?有任何想法嗎?
uj5u.com熱心網友回復:
問題是您CheckBox在黑色背景上使用默認值。復選標記因此不可見。
要更改顏色,您必須覆寫默認模板。您可以在 Microsoft Docs 中找到示例模板:CheckBox ControlTemplate Example。您想要調整"optionMark"和"indeterminateMark"元素的顏色,以及觸發器中定義的顏色(例如,滑鼠懸停等):
<CheckBox IsChecked="False"
HorizontalAlignment="Left">
<CheckBox.Template>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid x:Name="templateRoot"
Background="Transparent"
SnapsToDevicePixels="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border x:Name="checkBoxBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="1"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Grid x:Name="markGrid">
<Path x:Name="optionMark"
Data="F1 M 9.97498,1.22334L 4.6983,9.09834L 4.52164,9.09834L 0,5.19331L 1.27664,3.52165L 4.255,6.08833L 8.33331,1.52588e-005L 9.97498,1.22334 Z "
Fill="{TemplateBinding Foreground}"
Margin="1"
Opacity="0"
Stretch="None" />
<Rectangle x:Name="indeterminateMark"
Fill="Gray"
Margin="2"
Opacity="0" />
</Grid>
</Border>
<ContentPresenter x:Name="contentPresenter"
Grid.Column="1"
Focusable="False"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="true">
<Setter Property="Background"
TargetName="checkBoxBorder"
Value="white" />
<Setter Property="BorderBrush"
TargetName="checkBoxBorder"
Value="White" />
<Setter Property="Fill"
TargetName="optionMark"
Value="Black" />
<Setter Property="Fill"
TargetName="indeterminateMark"
Value="Black" />
</Trigger>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Background"
TargetName="checkBoxBorder"
Value="Gray" />
<Setter Property="BorderBrush"
TargetName="checkBoxBorder"
Value="Gray" />
<Setter Property="Fill"
TargetName="optionMark"
Value="Gray" />
<Setter Property="Fill"
TargetName="indeterminateMark"
Value="Gray" />
</Trigger>
<Trigger Property="IsPressed"
Value="true">
<Setter Property="Background"
TargetName="checkBoxBorder"
Value="White" />
<Setter Property="BorderBrush"
TargetName="checkBoxBorder"
Value="White" />
<Setter Property="Fill"
TargetName="optionMark"
Value="Black" />
<Setter Property="Fill"
TargetName="indeterminateMark"
Value="Black" />
</Trigger>
<Trigger Property="IsChecked"
Value="true">
<Setter Property="Opacity"
TargetName="optionMark"
Value="1" />
<Setter Property="Opacity"
TargetName="indeterminateMark"
Value="0" />
</Trigger>
<Trigger Property="IsChecked"
Value="{x:Null}">
<Setter Property="Opacity"
TargetName="optionMark"
Value="0" />
<Setter Property="Opacity"
TargetName="indeterminateMark"
Value="1" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/449652.html
