简体   繁体   English

如何将itemSource绑定到XAML中的列表

[英]How to bind an itemSource to a list in xaml

In C#: 在C#中:

namespace Slider
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    private Popup popup;
    private BackgroundWorker backroungWorker;
    public List<Pages> pages;

In XAML: 在XAML中:

<phone:PhoneApplicationPage
x:Class="Starz.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
etc....

<phone:PivotItem Header="Browse Pages">
            <!--Double line list no text wrapping-->
            <phone:LongListSelector x:Name="pages" Margin="0,0,-12,0"  ItemSource ="{Binding pages}">

The thing is that am in the MainPage and the ItemSource has a datacontext of MainViewModel. 事实是,它位于MainPage中,并且ItemSource具有MainViewModel的数据上下文。 what can I do to simply bind the longListSelector's itemSource to the list that I created in c# ??. 我可以做些什么来简单地将longListSelector的itemSource绑定到我在c#中创建的列表中? As you can see am still a beginner ..... Thanks in advance 如您所见,我仍然是初学者.....在此先感谢

You can't bind to a List or the View (xaml) will never be notified when the list change. 您无法绑定到列表,否则列表更改时将永远不会通知视图(xaml)。 That's why you have to use an ObservableCollection. 这就是为什么您必须使用ObservableCollection。

Here an exemple similar with the MVVM pattern and a datagrid : 这是一个与MVVM模式和datagrid类似的示例:

MVVM Exemple MVVM示例

Let's say you have got a windows with a datagrid : 假设您有一个带有datagrid的窗口:

<Window x:Class="WpfApplication7.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">
    <DataGrid ItemsSource="{Binding Personels}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Nom" Binding="{Binding Nom}" Width="200"></DataGridTextColumn> 
        </DataGrid.Columns>
    </DataGrid>
</Window>

We have to define his datacontext (or the binding won't know where to find) : 我们必须定义他的datacontext(否则绑定将不知道在哪里找到):

namespace WpfApplication7
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

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

Then we can define the ViewModel (datacontext of the windows) : 然后我们可以定义ViewModel(窗口的数据上下文):

class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Référence de la fenêtre principale
        /// </summary>
        private MainWindow mainWindow;

        /// <summary>
        /// Liste des personels
        /// </summary>
        private ObservableCollection<Personel> personels = new ObservableCollection<Personel>();


        public ObservableCollection<Personel> Personels
        {
            get 
            {
                return personels;
            }
        }

        /// <summary>
        /// Constructeur de la classe
        /// </summary>
        /// <param name="mainWindow">Référence de la fenêtre principale</param>
        public MainViewModel(MainWindow mainWindow)
        {
            this.mainWindow = mainWindow;

            AddPersonel("Toto");

            AddPersonel("Jack");

            AddPersonel("Momo");

            AddPersonel("Momo");

            AddPersonel("Momo");

            AddPersonel("Momo");
        }

        private void AddPersonel(string namePersonel)
        {
            personels.Add(new Personel() { Name = namePersonel });
            OnPropertyChanged("Personels");
        }
    }

    class Personel
    {
        private string name = "NoName";

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

MainViewModel must implement INotifyPropertyChanged to notifies controls that a property value has changed. MainViewModel必须实现INotifyPropertyChanged来通知控件属性值已更改。 :

public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

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

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