我ToggleButton的內容和內容有問題。我希望這些資訊足夠,如果您需要其他資訊,請告訴我!
問題
我有一個ToggleButton包含 aFontIcon和 aTextBlock的 a Grid:
<ToggleButton Grid.Row="1" HorizontalAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<FontIcon Grid.Column="0"
FontSize="20"
Glyph="{StaticResource mdi_handyman}"
FontFamily="{StaticResource MaterialDesignIconsOutlined}"/>
<TextBlock Grid.Column="1" Margin="10,0,0,0" Text="{x:Bind p:Resources.Dashboard_Maintenance}"/>
</Grid>
</ToggleButton>
中的文本ToggleButton應始終為白色。這就是為什么我為以下資源設定ToggleButton:
<SolidColorBrush x:Key="ToggleButtonForeground" Color="{ThemeResource WhiteColor}"/>
<SolidColorBrush x:Key="ToggleButtonForegroundDisabled" Color="{ThemeResource WhiteColor}"/>
<SolidColorBrush x:Key="ToggleButtonForegroundPointerOver" Color="{ThemeResource WhiteColor}"/>
<SolidColorBrush x:Key="ToggleButtonForegroundChecked" Color="{ThemeResource RichBlackColor}"/>
<SolidColorBrush x:Key="ToggleButtonForegroundCheckedPointerOver" Color="{ThemeResource RichBlackColor}"/>
<SolidColorBrush x:Key="ToggleButtonForegroundPressed" Color="{ThemeResource WhiteColor}"/>
<SolidColorBrush x:Key="ToggleButtonBackground" Color="{ThemeResource RichBlackColor}"/>
<SolidColorBrush x:Key="ToggleButtonBackgroundDisabled" Color="{ThemeResource RichBlackColor}"/>
<SolidColorBrush x:Key="ToggleButtonBackgroundPointerOver" Color="{ThemeResource LightPrussianBlueColor}"/>
<SolidColorBrush x:Key="ToggleButtonBackgroundChecked" Color="{ThemeResource VividSkyBlueColor}"/>
<SolidColorBrush x:Key="ToggleButtonBackgroundCheckedDisabled" Color="{ThemeResource PaleVividSkyBlueColor}"/>
<SolidColorBrush x:Key="ToggleButtonBackgroundCheckedPointerOver" Color="{ThemeResource VividSkyBlueColor}"/>
<SolidColorBrush x:Key="ToggleButtonBackgroundPressed" Color="{ThemeResource LightPrussianBlueColor}"/>
現在還需要在FontIcon其中添加Grid。但這FontIcon與文本的行為相同(顯然,ToggleButton前景的資源用于整個內容)。
但是,FontIcon需要以下行為ToggleButton:
<SolidColorBrush x:Key="ToggleButtonForeground" Color="{ThemeResource VividSkyBlue}"/>
<SolidColorBrush x:Key="ToggleButtonForegroundDisabled" Color="{ThemeResource VividSkyBlue}"/>
<SolidColorBrush x:Key="ToggleButtonForegroundPointerOver" Color="{ThemeResource VividSkyBlue}"/>
<SolidColorBrush x:Key="ToggleButtonForegroundChecked" Color="{ThemeResource RichBlackColor}"/>
<SolidColorBrush x:Key="ToggleButtonForegroundCheckedPointerOver" Color="{ThemeResource RichBlackColor}"/>
<SolidColorBrush x:Key="ToggleButtonForegroundPressed" Color="{ThemeResource WhiteColor}"/>
我猜
FontIcon我想我可以使用系結到選定狀態的前景的轉換器來解決這個問題,ToggleButton但是這個轉換器還需要禁用狀態,ToggleButton這使得這更加復雜。因為我認為我能夠讓這個運行我認為這可能有點 hacky 并且不令人滿意。
有誰知道如何更優雅地解決這個問題?
先感謝您!
uj5u.com熱心網友回復:
您可以使用Microsoft.Xaml.Behaviors.WinUI.Managed NuGet 包。
這是代碼,但我更改了顏色以檢查它是否有效。
<FontIcon
x:Name="ThisFontIcon"
Grid.Column="0"
FontSize="20"
Glyph="{StaticResource CheckBoxCheckedGlyph}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="PointerEntered">
<core:ChangePropertyAction
PropertyName="Foreground"
TargetObject="{Binding ElementName=ThisFontIcon}"
Value="HotPink" />
</core:EventTriggerBehavior>
<core:EventTriggerBehavior EventName="PointerExited">
<core:ChangePropertyAction
PropertyName="Foreground"
TargetObject="{Binding ElementName=ThisFontIcon}"
Value="{ThemeResource SystemAccentColor}" />
</core:EventTriggerBehavior>
<core:DataTriggerBehavior
Binding="{Binding IsChecked, ElementName=ThisToggleButton}"
Value="True">
<core:ChangePropertyAction
PropertyName="Foreground"
TargetObject="{Binding ElementName=ThisFontIcon}"
Value="{ThemeResource SystemChromeBlackHighColor}" />
</core:DataTriggerBehavior>
<core:DataTriggerBehavior
Binding="{Binding IsChecked, ElementName=ThisToggleButton}"
Value="False">
<core:ChangePropertyAction
PropertyName="Foreground"
TargetObject="{Binding ElementName=ThisFontIcon}"
Value="{ThemeResource SystemAccentColor}" />
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</FontIcon>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/511402.html
標籤:xmlwinui-3
上一篇:在WPF解決方案中找不到程式集“Microsoft.Expression.Drawing”
下一篇:如何讓事件冒泡
