My xaml …
<ListBox Margin="6,35,6,6" Name="lbLayers" SelectionMode="Extended" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Key,Mode=TwoWay}" />
<TextBlock Text="{Binding Value.Visible,Mode=TwoWay,StringFormat='Visible: {0}'}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
.. and my code is …
void GameEvents_MapCreated(object sender, Zider.EventArgs.MapEventArgs e)
{
HookMapLayerEvents(false);
this.map = e.Map;
HookMapLayerEvents(true);
this.lbLayers.ItemsSource = this.map.Layers;
}
this.map.layers is a generic dictionary of type (string, MapLayer(Tile))
When I set ItemSource on the listbox there are no items in the dictionary to start with. When I click a button that is when I add a map layer to
this.map.Layers.Add("key", new MapLayer<Tile>());
Also MapLayer implements INotifyPropertyChanged for it’s properties.
For the life of me I can’t seem to get the items to be displayed in the listbox.
The problem is that the value of
map.Layersdoesn’t change nor doesDictionary<TKey, TValue>implement theINotifyCollectionChangedinterface. Hence there is no way for theListBoxto know that any new item is available to display.If possible try change the
Layersproperty so that it exposes aObservableCollection<T>instead.Of course that could be a problem if you must have a dictionary. If you are only interested in ensuring Unique entries you could use a
HastSetof the keys to manage that. If you need the Key for look up then if there are few items a sequential search should do reasonably well.A full blown solution might be to implement an
ObservableDictionarythat has bothIDictionary<TKey, TValue>andINotifyCollectionChangedinterfaces. There are a few about if you search for “ObservableDictionary Silverlight”, just be careful that they actually implement the correct interfaces, its not good if its “observable” but not in a way compatable withItemsSource.