C# 如何将itemSource绑定到xaml中的列表
在C#中:C# 如何将itemSource绑定到xaml中的列表,c#,xaml,windows-phone-8,binding,C#,Xaml,Windows Phone 8,Binding,在C#中: 名称空间滑块 { 公共部分类主页:PhoneApplicationPage { //建造师 私人弹出窗口; 私人背景工作者背景工作者; 公共列表页; 在XAML中: namespace Slider { public partial class MainPage : PhoneApplicationPage { // Constructor private Popup popup; private BackgroundWorker backroungWork
名称空间滑块
{
公共部分类主页:PhoneApplicationPage
{
//建造师
私人弹出窗口;
私人背景工作者背景工作者;
公共列表页;
在XAML中:
namespace Slider
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
private Popup popup;
private BackgroundWorker backroungWorker;
public List<Pages> pages;
您不能绑定到列表,否则当列表更改时,视图(xaml)将永远不会收到通知。这就是为什么您必须使用ObservableCollection
这里有一个与MVVM模式和datagrid类似的示例:
<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}">
MVVM示例
假设您有一个带有datagrid的windows:
<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}">
然后我们可以定义ViewModel(窗口的datacontext):
namespace WpfApplication7
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel(this);
}
}
}
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; }
}
}
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));
}
}
}