c# - tabcontroller 中的两个选定选项卡

标签 c# wpf xaml mvvm tabs

我在使用 TabControl 时遇到问题,我设法(在某些特殊情况下)选择了两个选项卡标题(只有一个正文显示 afaik),而且我无法更改所选选项卡。

Screenshot

选定的选项卡具有粗体标题文本。

在这张图片中,“Ämnesinformation”和“R43”都被选中。

我的申请结构如下:

我有几点看法:

  • MainView:主视图,包含仅包含图像中一项的 TabControl。
  • SubstanceTabsView:MainView 中的每个选项卡都有一个。
  • SubstanceView 和 ClassificationView:第一个用于“Ämnesinformation”,其中每种物质只有一个。第二个可以有多个实例,如“R43”、“R12”等。

我还有一些viewModels:

  • MainViewModel:MainView 的 VM。
  • SubstanceTabsViewModel:SubstanceTabsView 的 VM,包含一组 IViewModel
  • SubstanceViewModel、ClassificationViewModel:都实现了 IViewModel,是 SubstanceView 和 ClassificationView 的 VM

一些相关的xaml代码:

这是 MainView.xaml 中的选项卡控件

<TabControl SelectedItem="{Binding Path=SelectedTab}" ItemsSource="{Binding Path=Tabs}" >
        <TabControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Header}" >
                    </TextBlock>
                    <local:CrossButton Margin="3" Padding="0" Width="12" Command="{Binding CloseCommand}"/>
                </StackPanel>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.Resources>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabItem}">
                            <Grid>
                                <Border 
                                Name="Border"
                                Margin="0,0,-4,0" 
                                Background="{Binding Path=HeaderBackground}"
                                BorderBrush="#A0A0A0" 
                                BorderThickness="1,1,1,1" 
                                CornerRadius="3,10,0,0" >
                                    <ContentPresenter x:Name="ContentSite"
                                  VerticalAlignment="Center"
                                  HorizontalAlignment="Center"
                                  ContentSource="Header"
                                  Margin="12,2,12,2"
                                  RecognizesAccessKey="True"/>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="FontWeight" Value="Bold" />
                                    <Setter Property="Panel.ZIndex" Value="100" />
                                    <Setter TargetName="Border" Property="Background" Value="{Binding HeaderBackground}" />
                                    <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter TargetName="Border" Property="Background" Value="Yellow" />
                                    <Setter TargetName="Border" Property="BorderBrush" Value="Black" />
                                    <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
                                    <Setter Property="Foreground" Value="Green" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <DataTemplate DataType="{x:Type localViewModels:SubstanceTabsViewModel}">
                <localViews:SubstanceTabsView />
            </DataTemplate>
            </TabControl.Resources>

    </TabControl>

下面是我如何在 SubstanceTabsView.xaml 中控制不同 View 和 View 模型之间的连接

<TabControl SelectedItem="{Binding Path=SelectedTab}"  ItemsSource="{Binding Path=Tabs}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Header}" />
                    <local:CrossButton Margin="3" Padding="0" Width="12" Command="{Binding CloseCommand}"/>
                </StackPanel>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.Resources>
            <DataTemplate DataType="{x:Type localViewModels:ClassificationViewModel}">
                <localViews:ClassificationView />
            </DataTemplate>
            <DataTemplate DataType="{x:Type localViewModels:SubstanceViewModel}">
                <localViews:SubstanceView />
            </DataTemplate>
        </TabControl.Resources>
    </TabControl>

这是控制二级选项卡的 SubstanceTabsViewModel.cs 的代码,selectedTab 的 setter 控制一些逻辑,询问用户是否从未保存的选项卡进行更改:

private IViewModel selectedTab;
    public IViewModel SelectedTab
    {
        get
        {
            return selectedTab;
        }
        set
        {
            MessageBoxResult rsltMessageBox = MessageBoxResult.Yes;
            if (selectedTab != null && selectedTab.SaveNeeded() && selectedTab.Id != 0 && value != null && selectedTab is ClassificationViewModel)
            {
                rsltMessageBox = notifyUserService.Ask("Bedömning är ändrad men ej sparad vill du verkligen lämna fliken?", "Bedömning ändrad");
            }
            if (rsltMessageBox == MessageBoxResult.Yes)
            {
                selectedTab = value;
            }
            OnPropertyChanged("SelectedTab");

        }
    }

    private ObservableCollection<IViewModel> tabs;
    public ObservableCollection<IViewModel> Tabs
    {
        get
        {
            return tabs;
        }
        set
        {
            tabs = value;
            OnPropertyChanged("Tabs");
        }
    }

我的调查得出的一些结果:如果我不执行 notifyUserService 调用(导致 messagebox.show()),则没有问题,只选择了一个选项卡。如果我查看 TabControl 的 SelectedItem,它只是一个项目,它“应该”符合我的情况。

最佳答案

我终于发现其他人也有类似的问题,如所述here **, "显示消息框会导致嵌套的消息泵;这意味着几乎所有的处理都会恢复。当然,我们正在尝试更改所选项目,因此这可能会导致各种乱序或重入问题。这类问题很难解决,我们将无法在下一个版本中解决这个问题。”所以问题在于在 selectedItem setter 中使用 MessageBox:es。

我想在这种情况下使用一些聪明的解决方法是合适的解决方案。

** 2022 年 3 月更新

  • 原始帖子引用的 URL 不再有效。现在可以在此处找到内容:WPF TabControl bug

关于c# - tabcontroller 中的两个选定选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25997785/

相关文章:

c# - 如何使取消按钮像 "X"按钮一样工作?

c# - Tic Tac Toe 的 Minimax 算法

c# - 使用 LINQ 查找最常用的单词

c# - 由于某些未知原因,多次意外调用点击事件函数

c# - 在 WPF MVVM 中使用 ReactiveUI 获取属性更改的先验值

c# - 如何将 WPF 控件的边距绑定(bind)到属性?

c# - 删除列表c#中的重复引用

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException 未处理

wpf - 检测组合键

wpf - C# - XAML 中的 WPF 反向绑定(bind) ObservableCollection