gpt4 book ai didi

xaml - AvaloniaUI 样式-伪类

转载 作者:行者123 更新时间:2023-12-04 12:09:39 26 4
gpt4 key购买 nike

我正在 avalonia 和大多数作品中尝试样式,除了伪类,它们只是被忽略了。
我创建了一个窗口,所有样式都在那里,我创建了一个用户控件(带有一个按钮 - 伪类在按钮上),使用样式。我不使用代码,只使用 xaml 来定义样式。
我已经在“样式选择器”中将按钮作为“Button:pseudoclassname”和“Button.le:pseudoclassname”进行了尝试。我也尝试过“Button:pointerover”和“Button:hover”,因为文档提到可以修改。没有结果。伪类的样式全部被忽略,其他的都正确执行。
是我做错了什么还是 avalonia 中的错误?
xaml 窗口文件:

<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:Hub.Views"
xmlns:vm="using:Hub.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Hub.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Width="200" Height="300"
Title="Hub"
Classes="le">

<Window.Styles>
<Style Selector="Window.le">
<Setter Property="Background" Value="#191919"/>
</Style>
<Style Selector="Button.le">
<Setter Property="Background" Value="green"/>
<Setter Property="Foreground" Value="white"/>
</Style>
<Style Selector="Button.le:pointerover">
<Setter Property="Background" Value="red"/>
<Setter Property="Foreground" Value="white"/>
</Style>
<Style Selector="Button.le:pressed">
<Setter Property="Background" Value="blue"/>
<Setter Property="Foreground" Value="white"/>
</Style>
<Style Selector="TextBlock.le_update">
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="FontSize" Value="17"/>
<Setter Property="FontFamily" Value="Arial, Verdana"/>
<Setter Property="Foreground" Value="white"/>
<Setter Property="Background" Value="transparent"/>
</Style>
</Window.Styles>

<views:UpdateView/>

</Window>

xaml 用户控制文件:

<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Hub.Views.UpdateView">
<DockPanel>
<StackPanel DockPanel.Dock="Bottom">
<Button Classes="le" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Width="300">
Check for updates
</Button>
</StackPanel>
<StackPanel Spacing="5" Margin="5">
<TextBlock Classes="le_update" HorizontalAlignment="Center" Margin="4">
No updates for Hub.
</TextBlock>
<TextBlock Classes="le_update" HorizontalAlignment="Center" Margin="4">
No updates for Engine.
</TextBlock>
</StackPanel>
</DockPanel>
</UserControl>

最佳答案

ButtonBackground属性确实会受到您的风格的影响,但此时它对实际 Button 没有影响。的背景,因为 Background属性仅控制按钮的正常状态。
如果你看一下默认的 Button款式here你可以看到它把它的背景传给了 ContentPresenter通过 TemplateBinding :

    <Setter Property="Template">
<ControlTemplate>
<ContentPresenter x:Name="PART_ContentPresenter"
Background="{TemplateBinding Background}"
但以如下样式覆盖 ContentPresenter 的背景:
  <Style Selector="Button:pointerover /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundPointerOver}" />
既然是 ContentPresenter实际上绘制背景( Button 是一个无外观的控件),你会看到按钮有 ButtonBackgroundPointerOver而不是 ButtonBackground适当的值(value)。
因此,您需要编写一个样式来更改内部 ContentPresenter,如下所示:
<Style Selector="Button.le:pointerover /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="green" />
</Style>
不幸的是,这使您的样式依赖于主题,因为默认主题和流畅主题之间的控件模板不同。

关于xaml - AvaloniaUI 样式-伪类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66442508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com