簡體   English   中英

如何將itemSource綁定到XAML中的列表

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

在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的數據上下文。 我可以做些什么來簡單地將longListSelector的itemSource綁定到我在c#中創建的列表中? 如您所見,我仍然是初學者.....在此先感謝

您無法綁定到列表,否則列表更改時將永遠不會通知視圖(xaml)。 這就是為什么您必須使用ObservableCollection。

這是一個與MVVM模式和datagrid類似的示例:

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(窗口的數據上下文):

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));
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM