0

我有一个 ContentControl,它的 Content 使用 DataTemplateSelector 设置为 TabControl。ItemsSource 绑定到的项目具有 bool IsSelected 属性,选项卡的 IsSelected 属性使用 Style 中的 TwoWay Setter 链接到该属性。

我试图在单独的测试应用程序中重新创建问题,但无法重新创建它 - 选项卡是根据项目 IsSelected 属性按预期选择的。

我在发生此问题的主应用程序中看到的是:交替选择第一个选项卡,然后每次 ContentControl 设置其 DataContext 时不选择选项卡。通过删除 Setter,可以在以下示例中看到此行为。

我需要一些有关调试此问题的最佳方法的指导。如果有人想看一下,我可以提供主要的应用程序源。

    <Window x:Class="SelectedTab.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SelectedTab"
        Title="MainWindow" Height="350" Width="525">


    <Window.Resources>

        <local:NodeViewDataTemplateSelector x:Key="NodeViewDataTemplateSelector" />


        <DataTemplate x:Key="Tab">
            <TabControl ItemsSource="{Binding Nodes}">
                <TabControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Text}"></TextBlock>
                    </DataTemplate>
                </TabControl.ItemTemplate>



                <TabControl.ItemContainerStyle>
                    <Style TargetType="{x:Type TabItem}">
                        <!--Remove this Setter to see the behaviour that I am seeing in the main application-->
                        <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"></Setter>
                    </Style>
                </TabControl.ItemContainerStyle>

                <TabControl.ContentTemplate>
                    <DataTemplate>
                        <ContentControl Content="{Binding }" ContentTemplateSelector="{StaticResource NodeViewDataTemplateSelector}" ></ContentControl>
                    </DataTemplate>
                </TabControl.ContentTemplate>
            </TabControl>
        </DataTemplate>

    </Window.Resources>


    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">
            <Button Content="English" Click="ButtonOne_OnClick"/>
            <Button Content="German" Click="ButtonTwo_OnClick"/>
            <Button Content="French" Click="ButtonThree_OnClick"/>
        </StackPanel>


        <ContentControl Name="Content" Grid.Row="1" Content="{Binding }" ContentTemplateSelector="{StaticResource NodeViewDataTemplateSelector}" ></ContentControl>

    </Grid>
</Window>


    using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace SelectedTab
{
    public partial class MainWindow : Window
    {
        private Node nodeOne;
        private Node nodeTwo;
        private Node nodeThree;

        public MainWindow()
        {
            InitializeComponent();

            nodeOne = new Node();
            nodeOne.Nodes = new ObservableCollection<Node>();
            nodeOne.Nodes.Add(new Node { Text = "One" });
            nodeOne.Nodes.Add(new Node { Text = "Two", IsSelected = true });
            nodeOne.Nodes.Add(new Node { Text = "Three" });
            nodeOne.Nodes.Add(new Node { Text = "Four" });

            nodeTwo = new Node();
            nodeTwo.Nodes = new ObservableCollection<Node>();
            nodeTwo.Nodes.Add(new Node { Text = "Ein" });
            nodeTwo.Nodes.Add(new Node { Text = "Zwei" });
            nodeTwo.Nodes.Add(new Node { Text = "Drei", IsSelected = true });
            nodeTwo.Nodes.Add(new Node { Text = "Vier" });

            nodeThree = new Node();
            nodeThree.Nodes = new ObservableCollection<Node>();
            nodeThree.Nodes.Add(new Node { Text = "Un" });
            nodeThree.Nodes.Add(new Node { Text = "Deux" });
            nodeThree.Nodes.Add(new Node { Text = "Troix" });
            nodeThree.Nodes.Add(new Node { Text = "Quatre", IsSelected = true });
        }

        private void ButtonOne_OnClick(object sender, RoutedEventArgs e)
        {
            Content.DataContext = null;
            Content.DataContext = nodeOne;
        }

        private void ButtonTwo_OnClick(object sender, RoutedEventArgs e)
        {
            Content.DataContext = null;
            Content.DataContext = nodeTwo;
        }

        private void ButtonThree_OnClick(object sender, RoutedEventArgs e)
        {
            Content.DataContext = null;
            Content.DataContext = nodeThree;
        }
    }

    public class Node
    {
        public string Text { get; set; }

        public bool IsSelected { get; set; }

        public ObservableCollection<Node> Nodes { get; set; }
    }

    class NodeViewDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            Node node = item as Node;

            if (node != null && node.Nodes != null)
            {
                return Application.Current.MainWindow.FindResource("Tab") as DataTemplate;
            }

            return null;
        }
    }
}
4

0 回答 0