简体   繁体   English

WPF在选项卡更改时维护ListBox选择

[英]WPF Maintain ListBox selection when tab changes

I have a TabControl where the content of each TabItem is a master-details view. 我有一个TabControl,其中每个TabItem的内容都是主从视图。

For the master, I'm using a listbox whose ItemsSource is bound to a collection in my ViewModel. 对于主对象,我正在使用一个列表框,该列表框的ItemsSource绑定到ViewModel中的集合。 Selecting an item from the list displays that particular item's details in a grid off to the side. 从列表中选择一个项目会在旁边的网格中显示该特定项目的详细信息。

When I switch to another tab and then back to the original tab, the listbox selection seems to be lost. 当我切换到另一个选项卡然后返回到原始选项卡时,列表框选择似乎丢失了。

What can I do to maintain the listbox selections in each tab when the tab selection changes? 当选项卡选择发生更改时,如何维护每个选项卡中的列表框选择?

In normal use the end users will need to 'set up' the detail views the way they like for a particular situation, and then cycle through the tabs occasionally to check on each system (each tab provides details for machinery on a different product line). 在正常使用中,最终用户将需要根据自己的特定情况“设置”详细视图,然后不定期浏览选项卡以检查每个系统(每个选项卡提供不同产品线的机械的详细信息) 。

The TabControl looks like this: TabControl看起来像这样:

<TabControl 
    ItemsSource="{Binding DiagCards}"
    ContentTemplate="{StaticResource DiagCardViewTemplate}"
    SelectedItem="{Binding SelectedDiagCard}" />

The View for each TabItem has a ListBox that looks like this: 每个TabItem的View都有一个如下所示的ListBox:

<ListBox
   ItemsSource="{Binding DiagCard.DevicesDetected}" 
   SelectedItem="{Binding SelectedDevice}"/>

The details are displayed in the TabItem using a ContentControl: 使用ContentControl在TabItem中显示详细信息:

<ContentControl
   Content="{Binding SelectedDevice}"
   ContentTemplateSelector="{StaticResource SelectedDeviceTemplateSelector}"/>

I should note that a simple test using hard-coded TabItems and ListBoxes does seem to maintain the selection when the tab changes: 我应该注意,当选项卡更改时,使用硬编码的TabItems和ListBoxes的简单测试似乎确实可以保持选择:

    <TabControl>
        <TabItem Header="tab 1">
            <ListBox>
                <ListBoxItem>
                    <TextBlock Text="item 1-1"/>
                </ListBoxItem>
                <ListBoxItem>
                    <TextBlock Text="item 1-2"/>
                </ListBoxItem>
            </ListBox>
        </TabItem>
        <TabItem Header="tab 2">
            <ListBox>
                <ListBoxItem>
                    <TextBlock Text="item 2-1"/>
                </ListBoxItem>
                <ListBoxItem>
                    <TextBlock Text="item 2-2"/>
                </ListBoxItem>
            </ListBox>
        </TabItem>
    </TabControl>

Update : I set IsSynchronizedWithCurrentItem="True" on the listbox and all seems to be well. 更新 :我在列表框中设置了IsSynchronizedWithCurrentItem="True" ,而且一切似乎都很好。

Based on your comment I would guess something is changing in your viewmodel to remove the selected item. 根据您的评论,我想您的视图模型将发生某些更改以删除所选项目。 Can you set a breakpoint on the setter of SelectedDevice and check the call stack? 您可以在SelectedDevice的设置器上设置断点并检查调用堆栈吗?

To test that can you remove the SelectedItem={} code and see if it stays selected when you change tabs? 要测试是否可以删除SelectedItem = {}代码,并在更改选项卡时查看它是否保持选中状态?

To answer more completely I would need to see more code. 为了更完整地回答,我将需要查看更多代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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