使用数据模板做一个简单的LISTBOX

看一下页面文档

<Window x:Class="WpfApp1_test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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:local="clr-namespace:WpfApp1_test"
        mc:Ignorable="d"
        Title="自定义Listbox" Height="500" Width="800" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <DataTemplate x:Key="dateTemplate">
            <StackPanel Orientation="Horizontal">
                <Image Margin="5" Width="50" Height="50" Source="{Binding path}"/>
                <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5">
                    <TextBlock FontSize="20" Text="{Binding txt}"></TextBlock>
                    <TextBlock Text="{Binding tst}" Foreground="Gray"></TextBlock>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListBox Name="list" ItemTemplate="{StaticResource dateTemplate}">
        </ListBox>
    </Grid>
</Window>

在文档内数据末班就使用了一个StackPanel 设置了每项菜单的内部样式(一个Image和两个TextBlock)
看一下效果图:
在这里插入图片描述
是不是有点啥内味了呢。接着分享一下cs代码,也简单:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace WpfApp1_test
{
    public delegate void ShowMessageService(string msg);
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public delegate void ShowCounter(string Counter);
        public MainWindow()
        {
            InitializeComponent();
            List<addTXT> addTXTs = new List<addTXT>();
            addTXTs.Add(new addTXT { path = @"C:\Users\76621\source\repos\WpfApp1-test\WpfApp1-test\bin\Debug\netcoreapp3.1\1.png", txt = "G-Dragon&Nell-Today", tst = "G-Dragon&Nell; " });
            addTXTs.Add(new addTXT { path = @"C:\Users\76621\source\repos\WpfApp1-test\WpfApp1-test\bin\Debug\netcoreapp3.1\1.png", txt = "逆战", tst = "张杰" });
            addTXTs.Add(new addTXT { path = @"C:\Users\76621\source\repos\WpfApp1-test\WpfApp1-test\bin\Debug\netcoreapp3.1\1.png", txt = "清平雨下", tst = "许嵩" });
            addTXTs.Add(new addTXT { path = @"C:\Users\76621\source\repos\WpfApp1-test\WpfApp1-test\bin\Debug\netcoreapp3.1\1.png", txt = "剑心", tst = "李易峰" });
            addTXTs.Add(new addTXT { path = @"C:\Users\76621\source\repos\WpfApp1-test\WpfApp1-test\bin\Debug\netcoreapp3.1\1.png", txt = "SOBER", tst = "BIGBANG" });
            addTXTs.Add(new addTXT { path = @"C:\Users\76621\source\repos\WpfApp1-test\WpfApp1-test\bin\Debug\netcoreapp3.1\1.png", txt = "G-Dragon&Nell-Today", tst = "G-Dragon&Nell; " });
            list.ItemsSource = addTXTs;
        }
        class addTXT
        {
            public string path { get; set; }
            public string txt { get; set; }
            public string tst { get; set; }
        }
    }
}

在代码中定义了一个实体类里面包含了前面文档中要获取的参数,接着将实体列绑定到泛型列表中,操作列表源与实体类的列绑定即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF ListBox 绑定模板指的是在 ListBox使用数据绑定来绑定数据源,并使用自定义的模板来呈现数据。 以下是实现 WPF ListBox 绑定模板的步骤: 1. 创建数据源:创建一个集合类,用于存储数据源。 2. 绑定数据源:使用 ListBox 的 ItemsSource 属性将数据源绑定到 ListBox 控件上。 3. 创建数据模板:通过创建一个 DataTemplate 对象来定义自定义模板。 4. 应用模板使用 ListBox 的 ItemTemplate 属性将模板应用到 ListBox 控件上。 下面是一个示例代码,演示如何实现 WPF ListBox 绑定模板: ``` <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ListBox ItemsSource="{Binding Students}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding Age}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window> ``` 在上述代码中,我们创建了一个 ListBox 控件,并将它的 ItemsSource 属性绑定到一个名为 Students 的集合类上。然后,我们创建了一个 DataTemplate 对象,并在其中定义了一个 StackPanel 和两个 TextBlock 控件,用于显示每个学生的姓名和年龄。最后,我们将模板应用到 ListBox 中,以便呈现数据。 注意:在代码中,我们使用数据绑定和 MVVM 模式,这里不再赘述。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值