惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
L
LINUX DO - 最新话题
T
Troy Hunt's Blog
博客园_首页
量子位
Jina AI
Jina AI
S
SegmentFault 最新的问题
IT之家
IT之家
Hacker News - Newest:
Hacker News - Newest: "LLM"
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
S
Securelist
Google Online Security Blog
Google Online Security Blog
P
Privacy International News Feed
博客园 - Franky
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
C
Cisco Blogs
V
Vulnerabilities – Threatpost
腾讯CDC
The Hacker News
The Hacker News
K
Kaspersky official blog
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Security Archives - TechRepublic
Security Archives - TechRepublic
A
About on SuperTechFans
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
Scott Helme
Scott Helme
B
Blog
Security Latest
Security Latest
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - dalgleish

C# Avalonia 23- OpenGL- 例子通用类 C# Avalonia 22- SoundAndVideo- SpeechVoiceTest C# Avalonia 22- SoundAndVideo- SpeechRecognition C# Avalonia 22- SoundAndVideo- SpeechSynthesis C# Avalonia 22- SoundAndVideo- CodePlayback C# Avalonia 22- SoundAndVideo- SoundPlayerTest C# Avalonia 21- MenusAndToolbars- RibbonTest C# Avalonia 21- MenusAndToolbars- ProportionalStatusBar C# Avalonia 21- MenusAndToolbars- BasicToolbar C# Avalonia 21- MenusAndToolbars- ToolBar/Ribbon开源项目介绍和修复 C# Avalonia 21- MenusAndToolbars- SidebarMenu C# Avalonia 21- MenusAndToolbars- MixedMenus C# Avalonia 20 - WindowsMenu- 魔改Hyperlink - 使用例子 C# Avalonia 20 - WindowsMenu- 魔改Hyperlink C# Avalonia 20 - WindowsMenu- WebViewTest C# Avalonia 20 - WindowsMenu- WebView C# Avalonia 20 - WindowsMenu- ModernWindowTest C# Avalonia 20 - WindowsMenu- ModernWindow C# Avalonia 20 - WindowsMenu- TransparentWithShapes C# Avalonia 20 - WindowsMenu- TransparentBackground C# Avalonia 20 - WindowsMenu- OpenFileTest C# Avalonia 20 - WindowsMenu- SavePostion C# Avalonia 20 - WindowsMenu- CenterScreen C# Avalonia 19- DataBinding- DataGridGrouping C# Avalonia 19- DataBinding- DirectoryTreeView C# Avalonia 19- DataBinding- BoundTreeView
C# Avalonia 19- DataBinding- CustomListViewTest
dalgleish · 2026-02-14 · via 博客园 - dalgleish

CustomListViewTest.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="375" Width="525"
        xmlns:local="using:AvaloniaUI.Demos.Book._19.StoreDatabase"
        xmlns:main="using:AvaloniaUI"
         x:DataType="main:CustomListViewTest"
        x:Class="AvaloniaUI.CustomListViewTest"
        Title="CustomListViewTest">

    <Window.Resources>
        <local:ImagePathToBitmapConverter x:Key="ImagePathConverter" />
    </Window.Resources>
    <Grid Margin="5" RowDefinitions="*,auto">
        
        <!-- 表格视图:DataGrid -->
        <DataGrid Name="gridView"
                  HeadersVisibility="None"
                  ItemsSource="{Binding Products}"
                  AutoGenerateColumns="False"
                  IsReadOnly="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name"
                                    Binding="{Binding ModelName}"/>
                <DataGridTextColumn Header="Model"
                                    Binding="{Binding ModelNumber}" />
                <DataGridTextColumn Header="Price" IsReadOnly="True"
                                    Binding="{Binding UnitCost, StringFormat='{}{0:C}'}" />
            </DataGrid.Columns>
        </DataGrid>


        <!-- 图片缩略图视图 -->
        <ListBox Name="imageView"
                 Grid.Row="0"
                 ItemsSource="{Binding Products}"
                 IsVisible="False">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Width="150" VerticalAlignment="Top">
                        <Image Source="{Binding ProductImagePath, Converter={StaticResource ImagePathConverter}}" />
                        <TextBlock Text="{Binding ModelName}"
                                   TextWrapping="Wrap"
                                   HorizontalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>


        <!-- 图片 + 详情视图 -->
        <ListBox Name="imageDetailView"
                 Grid.Row="0"
                 ItemsSource="{Binding Products}"
                 IsVisible="False">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid ColumnDefinitions="auto,2*">
                        <Image Margin="5"
                               Width="100"
                               Source="{Binding ProductImagePath, Converter={StaticResource ImagePathConverter}}" />
                        <StackPanel Grid.Column="1"
                                    VerticalAlignment="Center">
                            <TextBlock FontWeight="Bold"
                                       Text="{Binding ModelName}" />
                            <TextBlock Text="{Binding ModelNumber}" />
                            <TextBlock Text="{Binding UnitCost, StringFormat='{}{0:C}'}" />
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>
        
        <!-- 视图切换 -->
        <Grid Grid.Row="1"
              Margin="5"
              ColumnDefinitions="auto,*">

            <TextBlock Margin="5"
                       VerticalAlignment="Center"
                       Text="Choose your view:" />

            <ComboBox Name="lstView"
                      Grid.Column="1"
                      Margin="5"
                      Width="150"
                      HorizontalAlignment="Left"
                      SelectionChanged="lstView_SelectionChanged">
                <ComboBoxItem Content="GridView" />
                <ComboBoxItem Content="ImageView" />
                <ComboBoxItem Content="ImageDetailView" />
            </ComboBox>

        </Grid>
    </Grid>
</Window>

CustomListViewTest.axaml.cs代码

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

namespace AvaloniaUI;

public partial class CustomListViewTest : Window
{
    private StoreDb1 db = new StoreDb1();
    public ObservableCollection<Product> Products { get; set; }
    public CustomListViewTest()
    {
        InitializeComponent();
        Products = db.GetProducts();
        this.DataContext = this;

        // 默认使用表格视图
        lstView.SelectedIndex = 0;
        UpdateView("GridView");
    }

    private void lstView_SelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        var selected = (lstView.SelectedItem as ComboBoxItem)?.Content as string;
        UpdateView(selected);
    }

    private void UpdateView(string? name)
    {
        gridView.IsVisible = name == "GridView";
        imageView.IsVisible = name == "ImageView";
        imageDetailView.IsVisible = name == "ImageDetailView";
    }
}

运行效果

image