0

I need to show the Key values of a dictionary in my application so that you see on which step of the list you are. So I thought an itemscontrol would be the best choice to go with. It didn't work out well. The Binding seems not to work at all. I haven't found any solution on google that matches my problem, at all solution the general binding seems to work...

I have following code, to demonstrate that the binding should work correctly I tried the same with a DataGrid and a ListView, both work fine:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding WizardSteps}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Key}" Header="Key" />
        <DataGridTextColumn Binding="{Binding Value}" Header="Value"/>
    </DataGrid.Columns>
</DataGrid>
<ListView ItemsSource="{Binding WizardSteps}">
    <ListView.ItemTemplate>
         <DataTemplate>
             <TextBlock Text="{Binding Key}"></TextBlock>
         </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

But this code doesn't work (no items):

<ItemsControl ItemsSource="{Binding WizardSteps}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
             <TextBlock Text="{Binding Key}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

C#:

public Dictionary<string, PageViewModelBase> WizardSteps { get; set; } = 
    new Dictionary<string, PageViewModelBase>();

private void _CreateWizardSteps()
{
    CreateWizardStepsSpecific(WizardSteps);
    WizardSteps.Add("Report", new ReportStepViewModel<ReportView>());
    WizardSteps.Add("Report1", new ReportStepViewModel<ReportView>());
    NotifyOfPropertyChange(() => WizardSteps);
}

I can't explain this to me, shouldn't the itemscontrol work the same way as the listview?

stop-cran
  • 4,229
  • 2
  • 30
  • 47
Hans Dabi
  • 159
  • 13
  • What class belongs the C# code to? Is it code part of your window? – stop-cran Jun 17 '16 at 06:59
  • @stop-cran no it's the datacontext of the usercontrol – Hans Dabi Jun 17 '16 at 07:00
  • @ViVi the datagird works fine, the only problem is with the itemscontrol where no items appear. – Hans Dabi Jun 17 '16 at 07:01
  • What does the `ItemsControl` one produce? Does it not generate the two textblocks? Or maybe the textblocks are there without the texts? – Jai Jun 17 '16 at 07:04
  • I'm looking for solving it – Guillaume P. Jun 17 '16 at 07:04
  • @Jai The textblocks aren't there, I checked it with an additional textbox that had a static value. – Hans Dabi Jun 17 '16 at 07:08
  • 1
    What is your problem, because it works perfectly here. – Guillaume P. Jun 17 '16 at 07:11
  • "shouldn't the itemscontrol work the same way as the listview?" Yes, it should, and actually it does. There is probably something else in your code you haven't shown. As it stands now your problem can't be reproduced. – Clemens Jun 17 '16 at 07:17
  • @GuillaumeP. wtf... did you try my code? I'm trying this for several hours now and also can't explain to me why it's not working. – Hans Dabi Jun 17 '16 at 07:18
  • In case you are changing the dictionary after the bindings are created, you should use an [ObservableDictionary](http://stackoverflow.com/q/10616554/1136211). – Clemens Jun 17 '16 at 07:21
  • Do you have a Panel (Grid or something) wrapping the ItemsControl? – Jai Jun 17 '16 at 07:22
  • @HansDabi Yes I tried it, and It works, let me copy paste it as a response – Guillaume P. Jun 17 '16 at 07:24
  • 1
    @HansDabi As said above, you must use an observableCollection to inform view that item sources has been updated. – Guillaume P. Jun 17 '16 at 07:28
  • Not repro for me. Try to set a simple data context for the `ItemsControl` to clarify the issue - is it related to binding or the context itself. – stop-cran Jun 17 '16 at 07:30
  • A workaround might be to create a new dictionary instance in _CreateWizardSteps() by `WizardSteps = new Dictionary();`. – Clemens Jun 17 '16 at 07:33
  • @Clemens yes but it's not the best to way to achieve on. On a few list of items it works, but on a huge collection :/ – Guillaume P. Jun 17 '16 at 07:37
  • @Clemens you're right. it was just to inform him about weakness of this. It is a good workaround instead. – Guillaume P. Jun 17 '16 at 07:41

1 Answers1

1

Found the solution. I had to use an ObservableDictionary instead of a normal one. Doesn't explain to me why it worked with the DataGrid and the ListView but the point is I solved it :) Thanks to all who helped me.

Hans Dabi
  • 159
  • 13
  • A possible explanation is timing. The bindings in ListView and DataGrid might get created later than those in the ItemsControl, i.e. after _CreateWizardSteps() was called. In case _CreateWizardSteps() is only called once on startup, you may probably just set the DataContext after calling _CreateWizardSteps(). – Clemens Jun 17 '16 at 07:56