如果用INotifyPropertyChanged的像這樣建立自己的等級:
public class MyDataObject : INotifyPropertyChanged
{
private DateTime _SelectedDate;
public DateTime SelectedDate
{
get
{
return _SelectedDate;
}
set
{
_SelectedDate = value;
NotifyPropertyChanged("SelectedDate");
GetDataForDate();
}
}
private ObservableCollection<YourDataType> _Data;
public ObservableCollection<YourDataType> Data
{
get
{
return _Data;
}
set
{
_Data = value;
NotifyPropertyChanged("Data");
}
}
public void GetDataForDate()
{
// Your code here to fill the Data object with your data
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
然後你就可以在XAML中創建你的ObjectDataProvider和直接綁定到它。在你的資源:
<ObjectDataProvider x:Key="MyDataSource" ObjectType="{x:Type local:MyDataObject}" />
,然後綁定:
<DockPanel>
<toolkit:DatePicker SelectedDate="{Binding Path=SelectedDate, Mode=Default, Source={StaticResource MyDataSource}}"/>
<toolkit:DataGrid ItemsSource="{Binding Path=Data, Mode=Default, Source={StaticResource MyDataSource}}"/>
</DockPanel>
哇,這是..非顯而易見..;)謝謝,我覺得我得到它的要點。然而,我得到了一堆異常,它試圖實例化一個指定類型的對象 - 我怎樣才能讓它使用由我的DataContext表示的非靜態對象? – 2008-11-18 06:02:47
從XAML中刪除ObjectType屬性並將DataContext分配給ObjectInstance。 <窗口x:類= 「DataGridSort.Window1」 ... X:名稱= 「_這個」/> 你應該給一個名字到你的窗口。然後: ... –
sacha
2008-11-18 22:23:46