如何将itemSsource绑定到xaml中的列表

时间:2014-07-16 09:14:55

标签: c# xaml windows-phone-8 binding

在C#中:

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

在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}">

问题是我在MainPage中,而ItemSource有一个MainViewModel的datacontext。我该怎么做才能简单地将longListSelector的itemSource绑定到我在c#中创建的列表。你可以看到我还是初学者......提前致谢

1 个答案:

答案 0 :(得分:0)

您无法绑定到列表,或者列表更改时永远不会通知视图(xaml)。这就是你必须使用ObservableCollection的原因。

这里有一个类似于MVVM模式和数据网格的例子:

MVVM示例

假设您有一个带有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>

我们必须定义他的datacontext(或绑定不知道在哪里找到):

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

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

然后我们可以定义ViewModel(窗口的datacontext):

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必须实现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));
            }
        }
    }