如何通过XAML完全绑定列表框。 [英] How to bind listbox completely through XAML.

查看:51
本文介绍了如何通过XAML完全绑定列表框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过很长一段时间我开始在wpf工作,我正在努力通过完整的XAML绑定填充列表框,下面是我的代码。



这是我的收藏类< br $> b $ b

Hi after long time I started working in wpf, I am struggling with populating listbox through complete XAML binding below is my code.

Here is my collection class

<pre>namespace WPFInterview
{
    public class Collection
    {
        public  IEnumerable<Person> _collection{get; set;}
        public Collection()
        { 
         FillObservableCollection();
        }

        private void FillObservableCollection()
        {
            _collection = new ObservableCollection<Person> { new Person { Name = "Ram", Age = 10 }, new Person { Name = "Shyam", Age = 11 }, new Person { Name = "Raju", Age = 13 } };
            
            // lstMy.ItemsSource = _collection;      
        }

        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }

        }
     
    }
}





这是我的带绑定的XAML代码





Here is my XAML code with binding

<Window x:Class="WPFInterview.BindingElement"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
        xmlns:local="clr-namespace:WPFInterview"     
        Title="BindingElement" Height="300" Width="300">
    <Window.DataContext>
        <local:Collection/>
    </Window.DataContext>
    <Canvas>
        <ListBox Name="lstMy" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Canvas.Left="67" Canvas.Top="65" ItemsSource="{Binding _collection}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding Age}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Canvas>
</Window>





我无法将列表框变为现实



我尝试过的事情:



是的我试过但不能填充。



I am not able to poupulate listbox

What I have tried:

Yes I have tried but could not populate.

推荐答案

你的收藏品需要从ObservableCollection继承:



Your collection needs to inherit from ObservableCollection:

public ObservableCollection<MyObject> MyCollection { get; set; };

public MainWindow()
{
    this.InitializeComponents();
    this.DataContext = this;
    this.MyCollection = new ObservableCollection<MyObject>();
}





然后在您的XAML中,您可以设置 ItemsSource ={Binding Path = MyCollection }


实际上即使在设计时也会填充集合和列表框。



更简单的形式

Well actually that populates both the collection and the list box, even at design time.

A bit simpler form
<Window x:Class="WindowNameSpace.Window3"

        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:SomeLocalNamespace"

        mc:Ignorable="d"

        Title="Window3" Height="300" Width="300">
    <Window.DataContext>
        <local:Collection/>
    </Window.DataContext>
    <Grid>
        <ListBox ItemsSource="{Binding _collection}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding Age}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>



和班级


And the class

using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SomeLocalNamespace {
   public class Collection {
      public IEnumerable<Person> _collection { get; set; }
      public Collection() {
         FillObservableCollection();
      }
      private void FillObservableCollection() {
         _collection = new ObservableCollection<Person> {
            new Person { Name = "Ram", Age = 10 },
            new Person { Name = "Shyam", Age = 11 },
            new Person { Name = "Raju", Age = 13 }
         };

      }
      public class Person {
         public string Name { get; set; }
         public int Age { get; set; }

      }
   }
}



如果您在设计时或运行时打开表单,则会看到包含名称和年龄的三行。



原因是您已在Window的数据上下文中定义了该类。 WPF然后在使用时再次实例化这些对象,因此这实际上会导致创建集合。



然后再次填充构造函数中的集合和列表中的绑定box正在引用你刚刚填充的集合,所以一切正常。



但是,正如John Simmons指出的那样,你应该使用可观察的集合,以便UI元素对变化做出反应采集。这也意味着该类应该实现INotifyPropertyChanged。


If you open the form in design time or at run-time you see three rows containing the names and ages.

The reason is that you have defined the class in the data context of the Window. WPF then again instantiates these objects when used so this actually causes the collection to be created.

Then again you're populating the collection in constructor and the binding in the list box is referencing the collection you just populated so everything works.

However, as John Simmons pointed out, you should used observable collection so that the UI element reacts upon changes in collection. This also means that the class should implement INotifyPropertyChanged.


这篇关于如何通过XAML完全绑定列表框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆