使用xaml根据选定的选项卡显示选择控件

时间:2012-04-12 13:02:54

标签: c# wpf xaml

我有以下代码:

    private Dictionary<int, UserControl> tabControls = new Dictionary<int, UserControl>();

    public MainWindow()
    {
        InitializeComponent();

        tabControls[0] = new Panel1();
        tabControls[1] = new Panel2();
        tabControls[2] = new Panel3();
        tabControls[3] = new Panel4();
        tabControls[4] = new Panel5();
        tabControls[5] = new Panel6();
        tabControls[6] = new Panel7();
        tabControls[7] = new Panel8();
    }

    public object SelectedTab
    {
        //this is assigned from xaml binding
        set
        {
            OnCurrentTabChanged(tabControl.SelectedIndex);
        }
    }


    void OnCurrentTabChanged(int tabIndex)
    {
        if (dataDisplay != null)
        {
            dataDisplay.Children.Clear();
            dataDisplay.Children.Add(tabControls[tabIndex]);
        }
    }

每次用户选择不同的标签时,都会显示另一个控件。

有没有办法使用xaml简化这个?

我无法将控件本身放在标签控件

3 个答案:

答案 0 :(得分:2)

我之前使用另一个TabControl隐藏了它的标题和框架。然后,我只需将SelectedIndex绑定到您的其他标签SelectedIndex,然后将两者同步

<!-- TabControl without the TabHeaders -->
<Style x:Key="TabControl_NoHeadersStyle" TargetType="{x:Type TabControl}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <DockPanel>
                    <!-- This is needed to draw TabControls with Bound items -->
                    <StackPanel IsItemsHost="True" Height="0" Width="0" />

                    <ContentPresenter x:Name="PART_SelectedContentHost"
                                      ContentSource="SelectedContent" />
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后,您可以设置两个选项卡控件,每个控件都绑定到不同的源,并将SelectedIndex的一个绑定到另一个的SelectedIndex

<TabControl x:Name="MainTabControl" />

<TabControl ItemsSource="{Binding Panels}"
            SelectedIndex="{Binding ElementName=MainTabControl, Path=SelectedIndex}" 
            Style="{StaticResource TabControl_NoHeadersStyle}" />

另一种方法是将SelectedIndex绑定到代码隐藏中的某些内容,然后随时更改,在另一个公开要显示的面板的属性上引发PropertyChanged通知。

<TabControl SelectedIndex="{Binding SelectedTabIndex} />

<ContentControl Content="{Binding SelectedPanel}" />

并在

背后的代码中
public int SelectedTabIndex
{
    get { return _selectedTabIndex;}
    set
    {
        if (_selectedTabIndex != value)
        {
            _selectedTabIndex = value;
            RaisePropertyChanged("SelectedTabIndex");

            RaisePropertyChanged("SelectedPanel");
        }
    }
}

public UserControl SelectedPanel
{
    get { return tabControls[SelectedTabIndex]; }
}

答案 1 :(得分:1)

TabItem有一个你可以绑定的IsSelected属性,我认为这会简化语法。

 public bool TabIsSelected
 {
     get { return tabIsSelected; }
     set 
     {
          if (value && dataDisplay != null)
          {
              dataDisplay.Children.Clear();
              dataDisplay.Children.Add(tabControls[tabIndex]);
          }
          tabIsSelected = value;
     }

但是我仍然不明白为什么你不能把控件放在tabitem中?

答案 2 :(得分:0)

使用codebehind

void OnCurrentTabChanged(int tabIndex)
{
    if (dataDisplay != null)
    {
        UIElemnt[] pp = dataDisplay.Children.Cast<UIElement>().ToArray();
        Array.ForEach(pp, x=> x.visibility = Visibility.Collapsed); 
        pp[tabIndex].visibility = Visibility.Visible;
    }
}