我想寫一個簡單的2D地圖編輯器。這是我的代碼到目前爲止。如何編碼綁定到地圖的UserControl類?我似乎無法找到像處理ListBox和DataGrid一樣處理ItemsSource的UserControl示例。我想我需要找出什麼時候設置ItemsSource,然後編寫訂閱了CollectionChanged和PropertyChanged的代碼並創建/刪除/定位圖片?當我有3個ObservableCollections綁定時,我應該甚至試圖做到這一點?如何製作可以綁定到ItemsSource或DataContext的UserControl?
public class Map
{
public ObservableCollection<ObservableCollection<MapSquare>> Squares
= new ObservableCollection<ObservableCollection<MapSquare>>();
}
public class MapSquare
{
public ObservableCollection<MapTile> Items = new ObservableCollection<MapTile>();
}
public class MapTile : INotifyPropertyChanged
{
private CroppedBitmap bmp;
public CroppedBitmap Bitmap {
get{return bmp;}
set{ bmp = value; OnPropertyChanged("Bitmap");}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
我以爲我只是需要創建/刪除/更新項目 - 但在快速實現(不使用MVVM)後,我發現我肯定需要虛擬化。加載/更改50x100圖像的網格非常慢。我將在ScrollViewer中放置一個大的畫布,並在OnScroll事件中創建/移除Images,以便只有屏幕上實際存在的內容纔會出現。 – 2010-12-06 14:20:20