Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用xaml使基于选定选项卡的选择控件可见_C#_Wpf_Xaml - Fatal编程技术网

C# 使用xaml使基于选定选项卡的选择控件可见

C# 使用xaml使基于选定选项卡的选择控件可见,c#,wpf,xaml,C#,Wpf,Xaml,我有以下代码: private Dictionary<int, UserControl> tabControls = new Dictionary<int, UserControl>(); public MainWindow() { InitializeComponent(); tabControls[0] = new Panel1(); tabControls[1] = new Panel2()

我有以下代码:

    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]);
        }
    }
private Dictionary tabControls=new Dictionary();
公共主窗口()
{
初始化组件();
tabControls[0]=新的Panel1();
tabControls[1]=新面板2();
tabControls[2]=新面板3();
tabControls[3]=新面板4();
tabControls[4]=新面板5();
tabControls[5]=新面板6();
tabControls[6]=新面板7();
tabControls[7]=新面板8();
}
公共对象选择选项卡
{
//这是从xaml绑定分配的
设置
{
OnCurrentTabChanged(tabControl.SelectedIndex);
}
}
void OnCurrentTabChanged(int-tabIndex)
{
如果(数据显示!=null)
{
dataDisplay.Children.Clear();
dataDisplay.Children.Add(tabControls[tabIndex]);
}
}
每次用户选择不同的选项卡时,都会出现另一个控件

有没有办法使用xaml来简化这个过程

我无法使用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;
    }
}
void OnCurrentTabChanged(int-tabIndex)
{
如果(数据显示!=null)
{
UIElemnt[]pp=dataDisplay.Children.Cast().ToArray();
ForEach(pp,x=>x.visibility=visibility.Collapsed);
pp[tabIndex]。可见性=可见性。可见;
}
}
使用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;
    }
}
void OnCurrentTabChanged(int-tabIndex)
{
如果(数据显示!=null)
{
UIElemnt[]pp=dataDisplay.Children.Cast().ToArray();
ForEach(pp,x=>x.visibility=visibility.Collapsed);
pp[tabIndex]。可见性=可见性。可见;
}
}

我以前用另一个
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
绑定到代码隐藏中的某个内容,然后在它发生更改时,对另一个属性发出
PropertyChanged
通知,该属性将显示要显示的面板

<TabControl SelectedIndex="{Binding SelectedTabIndex} />

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

我以前用另一个
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
绑定到代码隐藏中的某个内容,然后在它发生更改时,对另一个属性发出
PropertyChanged
通知,该属性将显示要显示的面板

<TabControl SelectedIndex="{Binding SelectedTabIndex} />

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

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

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

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

tabitem有一个IsSelected属性,您可以绑定它,我认为这会简化语法

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

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

添加到
中有什么不对?您可以将面板放在选项卡项中,当选择其他选项卡时,非活动面板将隐藏。我遗漏了什么吗?。将
添加到您的
中有什么问题吗?您可以将面板放在选项卡项中,当选择其他选项卡时,非活动面板将隐藏。我遗漏了什么吗?-1“我无法将控件本身放入选项卡控件中”。。。您假设它们添加了,只是交换了可见性。-1“我不能将控件本身放在选项卡控件中”。。。您假定它们已添加,只需交换可见性即可。