首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在WPF中正确绑定ListBoxItem?

在WPF中,要正确绑定ListBoxItem,您需要遵循以下步骤:

  1. 创建一个数据模型类,该类将包含您要在ListBox中显示的数据。例如:
代码语言:csharp
复制
public class Item
{
    public string Name { get; set; }
    public string Description { get; set; }
}
  1. 在您的ViewModel或代码隐藏文件中,创建一个集合属性,该属性将包含数据模型类的实例。例如:
代码语言:csharp
复制
public ObservableCollection<Item> Items { get; set; }
  1. 将您的ListBox控件的ItemsSource属性绑定到您在第2步中创建的集合属性。例如:
代码语言:xml
复制
<ListBox ItemsSource="{Binding Items}">
  1. 为ListBoxItem的样式和数据模板创建一个DataTemplate。例如:
代码语言:xml
复制
<DataTemplate DataType="{x:Type local:Item}">
   <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Name}" />
        <TextBlock Text="{Binding Description}" />
    </StackPanel>
</DataTemplate>
  1. 在ListBox控件中应用此数据模板。例如:
代码语言:xml
复制
<ListBox ItemsSource="{Binding Items}" ItemTemplate="{StaticResource ItemTemplate}">

现在,您已经正确绑定了ListBoxItem,并且可以在ListBox中显示数据。

以下是完整的示例代码:

代码语言:xml<Window x:Class="WpfApp1.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"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
   <Window.Resources>
        <DataTemplate DataType="{x:Type local:Item}">
           <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" />
                <TextBlock Text="{Binding Description}" />
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding Items}" ItemTemplate="{StaticResource ItemTemplate}">
        </ListBox>
    </Grid>
</Window>
代码语言:csharp
复制
using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }

    public class ViewModel
    {
        public ObservableCollection<Item> Items { get; set; }

        public ViewModel()
        {
            Items = new ObservableCollection<Item>
            {
                new Item { Name = "Item 1", Description = "Description 1" },
                new Item { Name = "Item 2", Description = "Description 2" },
                new Item { Name = "Item 3", Description = "Description 3" }
            };
        }
    }

    public class Item
    {
        public string Name { get; set; }
        public string Description { get; set; }
    }
}

请注意,这个答案中没有提及任何云计算品牌商,因为这个问题是关于WPF中ListBoxItem绑定的问题。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

win10 uwp 如何使用DataTemplate 转换绑定Event到Command绑定 ObservableCollectionDataTemplate 绑定 ViewM

这是数据模板,一般用在数组的绑定,显示数组中的元素。 假如我们有一个列表,列表里是书,包括书名、作者、还有出版,那么我们只有源信息,如何把它显示到我们的ListView,就需要DataTemplate。 使用很简单,我们可以定义在资源,也可以定义在ItemTemplate。 数据模板有绑定的问题。 我们使用Binding和WPF其实没有多少不同,在Mode只有OneWay,OneTime,TwoWay。我们使用的x:bind在DataTemplate才和原来有一些不同。 我们使用x:bind需要我们对我们数据的类型,这个在前没有,我开始不知,弄了好久,最后才知道,还有一个,UWP默认是OneTime,也就是绑定只有一次。

02
领券