WPF绑定字典到ItemsControl不起作用

时间:2023-01-19 09:12:52

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和ListView,两者都工作正常:

<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#:

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?

我无法向我解释这一点,不应该将itemscontrol的工作方式与listview相同吗?

1 个解决方案

#1


0  

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.

找到了解决方案。我不得不使用ObservableDictionary而不是普通的。不解释为什么它与DataGrid和ListView一起使用但重点是我解决了它:)感谢所有帮助过我的人。

#1


0  

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.

找到了解决方案。我不得不使用ObservableDictionary而不是普通的。不解释为什么它与DataGrid和ListView一起使用但重点是我解决了它:)感谢所有帮助过我的人。