首页 > 其他分享 >AvaloniaUI(三、样式篇)

AvaloniaUI(三、样式篇)

时间:2023-08-01 15:56:11浏览次数:32  
标签:test1 test2 样式 Selector AvaloniaUI wpf 选择器

  avalonia的样式和wpf有不小的区别,它与css样式表很像,wpf的样式是使用定义的key 或者 类型 来使用样式的,avalonia是与css一样 是使用选择器来使用样式的,选择器有很多种,几乎与css的选择器一样,有类选择器,Id选择器,伪类选择器等,几乎常用的就那么几种,虽然他的使用方法与css类似,但是写法还是与wpf很相似了

类型选择器

<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"
             xmlns:vm="clr-namespace:test.ViewModels"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="test.Views.MainView"
             x:DataType="vm:MainViewModel">
  <Design.DataContext>
    <!-- This only sets the DataContext for the previewer in an IDE,
         to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
    <vm:MainViewModel />
  </Design.DataContext>
    <UserControl.Styles>
        <Style Selector="TextBlock">
            <Setter Property="Foreground" Value="Red"></Setter>
            <Setter Property="FontSize" Value="30"></Setter>
            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
        </Style>
    </UserControl.Styles>
    <StackPanel VerticalAlignment="Center">
        <TextBlock Text="{Binding Greeting}"/>
        <TextBlock>test1</TextBlock>
        <TextBlock>test2</TextBlock>
    </StackPanel>
  
    
</UserControl>

从上面的style的写法上看的出来,avalonia的样式只有个Selector 字段用来表明选择器的类型,与wpf不同,不需要写明 TargetType 上面的是类型选择器,Selector=“TextBlock” 表明所有的TextBlock都会应用这个样式

 

类选择器

<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"
             xmlns:vm="clr-namespace:test.ViewModels"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="test.Views.MainView"
             x:DataType="vm:MainViewModel">
  <Design.DataContext>
    <!-- This only sets the DataContext for the previewer in an IDE,
         to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
    <vm:MainViewModel />
  </Design.DataContext>
    <UserControl.Styles>
        <Style Selector="TextBlock.test">
            <Setter Property="Foreground" Value="Red"></Setter>
            <Setter Property="FontSize" Value="30"></Setter>
            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
        </Style>
    </UserControl.Styles>
    <StackPanel VerticalAlignment="Center">
        <TextBlock Text="{Binding Greeting}"/>
        <TextBlock Classes="test">test1</TextBlock>
        <TextBlock Classes="test">test2</TextBlock>
    </StackPanel>
  
    
</UserControl>

类选择器只需要在Selector="TextBlock.test" 加.test  使用的时候 需要使用 Classes=“test” 来标识一下就可以了

 

Id选择器

<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"
             xmlns:vm="clr-namespace:test.ViewModels"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="test.Views.MainView"
             x:DataType="vm:MainViewModel">
  <Design.DataContext>
    <!-- This only sets the DataContext for the previewer in an IDE,
         to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
    <vm:MainViewModel />
  </Design.DataContext>
    <UserControl.Styles>
        <Style Selector="TextBlock#test2">
            <Setter Property="Foreground" Value="Red"></Setter>
            <Setter Property="FontSize" Value="30"></Setter>
            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
        </Style>
    </UserControl.Styles>
    <StackPanel VerticalAlignment="Center">
        <TextBlock Text="{Binding Greeting}"/>
        <TextBlock Classes="test">test1</TextBlock>
        <TextBlock x:Name="test2">test2</TextBlock>
    </StackPanel>
  
    
</UserControl>

与类选择器类似,只不过.需要改成# 就是id选择器了 Selector=“TextBlock#test2” 使用的时候就不能用Classes了,需要加上x:Name=“test2”,这个x:Name才是id

 

现在问题来了,wpf的样式可以直接重写模板样式,avalonia可不可以呢?答案是 当然可以,这个时候就需要加上targettype 的关键字了

<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"
             xmlns:vm="clr-namespace:test.ViewModels"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="test.Views.MainView"
             x:DataType="vm:MainViewModel">
    <Design.DataContext>
        <!-- This only sets the DataContext for the previewer in an IDE,
         to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
        <vm:MainViewModel />
    </Design.DataContext>
    <UserControl.Styles>
        <Style Selector="TextBlock#test2">
            <Setter Property="Foreground" Value="Red"></Setter>
            <Setter Property="FontSize" Value="30"></Setter>
            <Setter Property="HorizontalAlignment" Value="Center"></Setter>
            <Setter Property="VerticalAlignment" Value="Center"></Setter>

        </Style>

        <Style Selector="TextBox#test3">
            <Setter Property="BorderBrush" Value="Red"></Setter>
            <Setter Property="BorderThickness" Value="1"></Setter>
            <Setter Property="Foreground" Value="Red"></Setter>
            <Setter Property="Width" Value="150"></Setter>
            <Setter Property="Height" Value="30"></Setter>
            <Setter Property="Tag" Value="test"></Setter>
            <Setter Property="Text" Value="23123"></Setter>
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}"
                                Width="{TemplateBinding Width}"
                                Height="{TemplateBinding Height}"
                                BorderThickness="{TemplateBinding BorderThickness}">

                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="auto"></ColumnDefinition>
                                    <ColumnDefinition></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="{TemplateBinding Tag}"
                                           VerticalAlignment="Center"
                                           HorizontalAlignment="Center"></TextBlock>
                                <Grid Grid.Column="1" Margin="5,0,0,0">
                                    <TextPresenter  Name="PART_TextPresenter"
                                                   
                                                   HorizontalAlignment="Left"
                                                   VerticalAlignment="{TemplateBinding VerticalAlignment}"
                                       CaretBrush="{TemplateBinding CaretBrush}"
                                       CaretIndex="{TemplateBinding CaretIndex}"
                                       LineHeight="{TemplateBinding LineHeight}"
                                       LetterSpacing="{TemplateBinding LetterSpacing}"
                                       PasswordChar="{TemplateBinding PasswordChar}"
                                       RevealPassword="{TemplateBinding RevealPassword}"
                                       SelectionBrush="{TemplateBinding SelectionBrush}"
                                       SelectionEnd="{TemplateBinding SelectionEnd}"
                                       SelectionForegroundBrush="{TemplateBinding SelectionForegroundBrush}"
                                       SelectionStart="{TemplateBinding SelectionStart}"
                                       Text="{TemplateBinding Text,Mode=TwoWay}"
                                       TextAlignment="{TemplateBinding TextAlignment}"
                                       TextWrapping="{TemplateBinding TextWrapping}" />
                                </Grid>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style Selector="TextBox#test3:pointerover:focus">
            <Setter Property="BorderBrush" Value="Red"></Setter>
            <Setter Property="BorderThickness" Value="1"></Setter>
            <Setter Property="Foreground" Value="Red"></Setter>

        </Style>

    </UserControl.Styles>
    <StackPanel VerticalAlignment="Center">
        <TextBlock Text="{Binding Greeting}"/>
        <TextBlock Classes="test">test1</TextBlock>
        <TextBlock x:Name="test2">test2</TextBlock>
        <TextBox x:Name="test3"></TextBox>
    </StackPanel>


</UserControl>

上面就重写了一个textbox文本框的样式,并加上了 前面的标题

 

今天就写到这里吧,我的鼠标老跟我捣乱,没心情了。。。。。

标签:test1,test2,样式,Selector,AvaloniaUI,wpf,选择器
From: https://www.cnblogs.com/wuyaxiansheng/p/17596715.html

相关文章

  • 2023-8-1 WPF的ItemsControl容器(DataGrid,ListBox,ListView等)可以实现的隔行样式修改
    实现的隔行样式修改【作者】长生微软官方文档详细介绍实现方式如果需要让你的wpf表格或者间隔样式实现下列效果可以使用AlternationCount首先添加一个DataGrid,并使用AlternatingRowBackground设置奇数行的背景色为紫色<DataGridAlternatingRowBackground="Purple"></Data......
  • css修改滚动条的样式
    滚动条样式的修改是通过伪元素实现的:-webkit-scrollbar​滚动条整体部分​-webkit-scrollbar-button​滚动条两端的按钮​-webkit-scrollbar-track​外层轨道​-webkit-scrollbar-track-piece​内层轨道,滚动条中间部分(除去)​-webkit-scrollbar-thumb​内嵌滑块​-webk......
  • Revit二次开发-用GDI+绘制线样式
      最近有个需求就是将动态讲当前文档中的线样式绘制出来,并且显示在自己的UI上。查阅了一下api文档以及吉米大佬的博客,并且这篇博客中受到启发填充样式绘制然后自己琢磨了一下,成功将线样式绘制出来了。但是目前有个问题就是圆点我在dashpattern里面设置的是1f的单位,如果图片......
  • less里面引入样式文件 @import怎么写
    在Less中引入样式文件的@import语句与常规的CSS相似。可以使用以下语法:less@import"path/to/file.less";或者less@importurl("path/to/file.less");其中,"path/to/file.less"是您要引入的样式文件的路径。请确保在使用@import语句时,文件扩展名为.less。请注意,当您在Less中引......
  • APIs修改样式
    1.可以通过style来改,但是太鸡肋样式一多就比较麻烦2.className和classList修改多样式是推荐使用后者优先使用因为其不影响其它类名classList.addclassList.removeclassList.toggle(有此类名就减去没有就加上)......
  • python网站创建007:常见CSS样式
    1.高度和宽度注意1:默认情况下高度和宽度无法应用在行内标签上注意2:默认情况下,块级标签虽然设置了宽度,但是右边空白区域是不允许占用的 高度<divstyle="height:100px"></div>宽度<divstyle="width:200px"></div>块级标签转换为行内标签<divstyle="display:in......
  • VisualStudio2022样式与快捷键与IDEA保持一致
    对于习惯了使用IntelliJIDEA的朋友们来说vs的快捷键和样式可能不太习惯,网上没有找到太多资料,所以我记录一下,一些快速修改VS样式和快捷键的技巧.本人使用的版本为VS2022社区版,也许2017以上都适用可以自行测试样式修改这个很简单安装一个插件就能实现:依次点击VS扩展>......
  • Angular: 样式绑定
    解决方案使用ngClass和ngStyle可以进行样式的绑定。ngStyle的使用ngStyle根据组件中的变量,isTextColorRed和fontSize的值来动态设置元素的颜色和字体大小<div[ngStyle]="{'color':isTextColorRed?'red':'blue','font-size':fontSize+'px'}"&g......
  • springboot启动中ccs样式和图片找不到, 报net::ERR_ABORTED 404
    1、 net::ERR_ABORTED404  项目结构 3、css错误的:<linkhref="/static/iconfont/style.css"type="text/css"rel="stylesheet">正确的:<linkhref="iconfont/style.css"type="text/css"rel="stylesh......
  • 通过js动态改变样式、改变伪类样式
    1、设置变量2、使用变量3、动态设置变量......