0

I'm having a torrid time with WPF and getting notification on bindings.

I've got a DLL (Called Population) that contains a class based on a dictionary that I am storing a custom class in:

public class WTRunRepository : Dictionary<int,WTRun>
{

}

I have other methods in the class that add or update the dictionary as needed, and these are referenced fine in the viewmodel that is using this dll.

I want to see what is in the dictionary dynamically in my view, so in my viewmodel I've added

    public Population Pop = new Population();

    public WTRunRepository Repository
    {
        get
        {
            return Pop.RunRepository;
        }
    }

I've then tried binding to it with my view:

            <ListBox ItemsSource="{Binding Repository}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Key}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

No joy. Still being relatively new to WPF I feel like pulling my hair out. Which bit is wrong? I've tried changing the Dictionary definition to implement a convoluted CollectionChanged Interface, but still nothing happens.

Help...

Rob Marsh
  • 549
  • 5
  • 22

1 Answers1

1

A Dictionary does not throw notifications... the only built in collection that throws the CollectionChanged event is ObservableCollection. You can either use both a dictionary and such a collection synced inside your application or use a custom dictionary implementation such as this: .NET ObservableDictionary

UPDATE Just went over your code again. and if in the Repository.RunRepository you are creating a new Repository then that is why your code does not work. When Binding to a Collection You can never again do Collection = new someOtherCollection because you will loose your binding target. You View will still be bindned to the old instance of the collection and thus will never be updated because you are acctually updating a new collection. You should always do collection.Clear and then add the new Items.

Community
  • 1
  • 1
Amit Raz
  • 5,370
  • 8
  • 36
  • 63
  • I've used the ObservableDictionary as suggested, and I can now at least bind a textbox to the Repository.Count value and watch it change as records are added. I still can't get the Listbox to bind to it though. Am I missing something fundamental? – Rob Marsh Jan 22 '15 at 11:37
  • What do you mean? How are you trying to do that? In your example the textbox should not bind to the Key as there is no key. Inisde the DataTemplate you need to bind to properties inside the WTRun object. – Amit Raz Jan 22 '15 at 12:19
  • I was just binding to the Dictionary Count property to see if it updated when a record was added, which it does. – Rob Marsh Jan 22 '15 at 12:29
  • so what is the problem now? – Amit Raz Jan 22 '15 at 12:29
  • I can't get the contents of the the dictionary to appear in my listbox at all. do you know the basic structure of the XAML for referencing a dictionary? – Rob Marsh Jan 22 '15 at 13:35
  • You should do ItemsSource="{Binding Repository.Values} – Amit Raz Jan 22 '15 at 13:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69392/discussion-between-rob-marsh-and-amit-raz). – Rob Marsh Jan 22 '15 at 13:43
  • Well so far the only way I've gotten anything to appear in my list is if I wrap my WTRun objects in an Observable Collection. My Dictionary is not causing any updates at all. Bit of a pain to have to keep updating that as well as the dictionary each time a change is made. – Rob Marsh Jan 22 '15 at 15:53