VariedStyles.axaml代码

<Window 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"
        Height="420" Width="675" 
        x:Class="AvaloniaUI.VariedStyles"
        xmlns:local="using:AvaloniaUI.Demos.Book._19.StoreDatabase"
        Title="VariedStyles">
    
    <Window.Styles>

        <Style Selector="TextBlock.travel-product">
            <Setter Property="Foreground" Value="OrangeRed"/>
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>

        <Style Selector="TextBlock.default-product">
            <Setter Property="Foreground" Value="Black"/>
        </Style>

    </Window.Styles>

    <ListBox x:Name="lstProducts">
        <ListBox.ItemTemplate>
            <DataTemplate x:DataType="local:Product">
                <StackPanel Margin="4">
                    <TextBlock Text="{Binding ModelName}"
                               Margin="0,0,0,2"
                               Classes.travel-product="{Binding IsTravel}"
                               Classes.default-product="{Binding !IsTravel}" />

                    <TextBlock Text="{Binding CategoryName}"
                               FontSize="11"
                               Opacity="0.7"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>

VariedStyles.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using AvaloniaUI.Demos.Book._19.StoreDatabase;

namespace AvaloniaUI;

public partial class VariedStyles : Window
{
    private StoreDb1 db = new StoreDb1();
    public VariedStyles()
    {
        InitializeComponent();
        lstProducts.ItemsSource = db.GetProducts();
    }
}

运行效果

image

 

posted on 2026-01-19 05:15  dalgleish  阅读(0)  评论(0)    收藏  举报