简体   繁体   中英

WPF Binding Dictionary to ItemsControl doesn't work

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM