这是一个使用 DependencyPropery 绑定 ObservableCollection 的小示例。我希望它可以帮助您了解如何以这种方式进行 DataBinding。
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="MyWindow"
Title="MainWindow">
<Grid>
<ListBox ItemsSource="{Binding MyList, ElementName=MyWindow, Mode=TwoWay}">
</ListBox>
</Grid>
</Window>
代码背后
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Logica di interazione per MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static readonly DependencyProperty MyListProperty = DependencyProperty.Register("MyList", typeof(ObservableCollection<string>), typeof(MainWindow));
public ObservableCollection<string> MyList
{
get
{
return (ObservableCollection<string>)GetValue(MyListProperty);
}
set
{
SetValue(MyListProperty, value);
}
}
public MainWindow()
{
InitializeComponent();
MyList = new ObservableCollection<string>() { "a", "b" };
MyList.Add("c");
}
}
}