我正在努力将 DataGrid 绑定到 SIlverlight 中的 ObservableCollection。
我非常简单的代码如下。它当前显示一个空白的 DataGrid。我已经完成了教程等,我确信我错过了一些非常基本的东西。
主页 XAML
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="Tower.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot">
<sdk:DataGrid Grid.Row="1" Margin="10" IsReadOnly="True" ColumnWidth="120" ItemsSource="{Binding Path=Tests, Mode=OneWay}" AllowDrop="True" />
</Grid>
主页代码背后:
public partial class MainPage : UserControl
{
public ObservableCollection<Test> Tests { get; set; }
public MainPage()
{
InitializeComponent();
DataContext = this;
Tests = new ObservableCollection<Test>();
Tests.Add(new Test() { Label = "Test1" });
Tests.Add(new Test() { Label = "Test2" });
Tests.Add(new Test() { Label = "Test3" });
Tests.Add(new Test() { Label = "Test4" });
}
}
测试类:
public class Test : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private String _Label;
public String Label
{
get
{
return _Label;
}
set
{
_Label = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Label"));
}
}
}