简体   繁体   English

WPF控件未显示在ItemTemplate(Listbox / ListView)中

[英]WPF controls not showing up in ItemTemplate(Listbox/ListView)

I have recently started with XAML and WPF. 我最近开始使用XAML和WPF。 I just created a new project in wpf and add the below XAML code. 我刚刚在wpf中创建了一个新项目,并添加了以下XAML代码。 But...none of the items which are added inside "Listbox.ItemTemplate" or "ListView.ItemTemplate" for that matter show up in designer window. 但是...为此在“ Listbox.ItemTemplate”或“ ListView.ItemTemplate”中添加的任何项目都不会显示在设计器窗口中。 what am i doing wrong? 我究竟做错了什么? This is a fresh project and so no code-behind stuff added yet. 这是一个新项目,因此尚未添加任何代码隐藏内容。 i scratched my head for 15 mins with this but with no success. 我为此划了15分钟,但没有成功。 Please help 请帮忙

<Window x:Class="WpfApplication3.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 Margin="10">
        <ListBox Margin="10">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Name: " />   
                        <TextBlock Text="Age: " /> 
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>    
    </Grid>
</Window>

在此处输入图片说明

You should bind your ListBox or entire Window to some DataContext (usually this is viewmodel with the data you need to display) or specify items of the list explicitly. 您应该将ListBox或整个Window绑定到某个DataContext(通常是带有需要显示的数据的viewmodel),或显式指定列表的项目。

In your snippet you specified only an item template , not the items itself. 在您的代码段中,您仅指定了项目模板 ,而不是项目本身。

The example of XAML-defined items (simple strings): XAML定义的项目(简单字符串)的示例:

<Window x:Class="WpfApplication3.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 Margin="10">
        <ListBox Margin="10">
            <ListBox.Items>
                <ListBoxItem>123</ListBoxItem>
                <ListBoxItem>456</ListBoxItem>
            </ListBox.Items>
        </ListBox>    
    </Grid>
</Window>

The example with DataContext and Bindings. 具有DataContext和Bindings的示例。 XAML: XAML:

<Window x:Class="WpfApplication3.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 Margin="10">
        <ListBox Margin="10" ItemsSource="{Binding Path=Persons}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label>Name:</Label><TextBlock VerticalAlignment="Center" Text="{Binding Path=Name}" />
                        <Label>Age:</Label><TextBlock VerticalAlignment="Center" Text="{Binding Path=Age}" /> 
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>    
    </Grid>
</Window>

Codebehind: 代码背后:

namespace WpfApplication3
{
    public class PersonViewModel
    {
        public PersonViewModel(string name, int age)
        {
            this.name = name;
            this.age = age;
        }

        public string Name
        {
            get { return name; }
        }
        private string name;

        public int Age
        {
            get { return age; }
        }
        private int age;
    }

    public class MainViewModel
    {
        public MainViewModel()
        {
            persons = new ObservableCollection<PersonViewModel>()
            {
                new PersonViewModel("Lez", 146),
                new PersonViewModel("Binja", 158),
                new PersonViewModel("Rufus the Destroyer", 9000)
            };
        }

        public ObservableCollection<PersonViewModel> Persons
        {
            get { return persons; }
        }

        private ObservableCollection<PersonViewModel> persons;
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = new MainViewModel();
        }
    }
}

IMPORTANT : Don't forget to properly implement INotifyPropertyChanged in case of mutable properties of viewmodels (for example, if you will have setters for "Name" and "Age" properties of PersonViewModel). 重要说明 :如果视图模型的属性可变(例如,如果您将拥有PersonViewModel的“名称”和“年龄”属性的设置器),请不要忘记正确实现INotifyPropertyChanged。

You don't see any items because your ListBox doesn't contain any data in the designer view. 您看不到任何项目,因为您的ListBox在设计器视图中不包含任何数据。 To populate it, a list should be binded to the property "ItemsSource" of your ListBox, or adding datas directly to the property "Items" (XAML or code behind). 若要填充它,列表应绑定到ListBox的属性“ ItemsSource”,或直接将数据添加到属性“ Items”(XAML或后面的代码)。

If you want to show items in the designer view properly, you should have a ViewModel binding to the DataContext of your page and create a "Sample data" file (via Blend for example). 如果要在设计器视图中正确显示项目,则应将ViewModel绑定到页面的DataContext并创建“样本数据”文件(例如,通过Blend)。

Item Templates are basically used to show customize data. 项目模板基本上用于显示自定义数据。 Just Use simple string items first. 只需先使用简单的字符串项。 It will show in list box. 它将显示在列表框中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM