开发者

silverlight 4 {binding} generic dictionary to listbox (items not being displayed)

My xaml ...

<ListBox Margin="6,35,6,6" Name="lbLayers" SelectionMode="Extended"    >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding Key,Mode=TwoWay}" />
 开发者_StackOverflow社区               <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.Layers doesn't change nor does Dictionary<TKey, TValue> implement the INotifyCollectionChanged interface. Hence there is no way for the ListBox to know that any new item is available to display.

If possible try change the Layers property so that it exposes a ObservableCollection<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 HastSet of 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 ObservableDictionary that has both IDictionary<TKey, TValue> and INotifyCollectionChanged interfaces. 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 with ItemsSource.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜