内容由DataTemplateSelector设置为TabControl的ContentControl未设置基于TwoWay Style Setter的选定选项卡

时间:2013-10-12 15:08:31

标签: wpf tabcontrol contentcontrol

我有一个ContentControl,它的内容设置为使用DataTemplateSelector的TabControl。 ItemsSource绑定的项目具有bool IsSelected属性,Tabs的IsSelected属性使用样式中的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;
        }
    }
}

0 个答案:

没有答案