以编程方式更新TabItem可见性时,不会触发IsVisibleChanged

时间:2014-01-04 19:57:50

标签: c# wpf xaml user-controls

我有一个带有三个选项卡的选项卡控件,其中两个选项卡的可见性设置为在程序启动时折叠,在某些条件下变为可见,以后可以再次折叠。如果正在折叠的TabItem是当前选定的选项卡,则即使内容已折叠,其内容仍然可见。

标签的可见性与我的ViewModel绑定,并以这种方式更新。

我总是希望在任何标签的可见性发生变化时激活第一个标签。我试图制作一个简单的代码来处理这种情况,但唯一一次代码被命中是我的UserControl加载/卸载时。更新选项卡的可见性时,永远不会调用Handler。我尝试在tabcontrol及其项目上设置IsVisibleChanged属性,但我无法让codebeind命中。

这是我的xaml:

    <UserControl x:Class="MyNameSpace.MyControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        IsVisibleChanged="TabControl_IsVisibleChanged>
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Platform.Presentation;component/Themes/MyTheme.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
<TabControl x:Name="_tabControl" IsVisibleChanged="TabControl_IsVisibleChanged">
    <TabItem Header="View 1" x:Name="_view1Tab" IsVisibleChanged="TabControl_IsVisibleChanged">
        <local:SingleWorkspaceView/>
    </TabItem>
    <TabItem Header="View 2" x:Name="_view2Tab" Visibility="{Binding TabVisibility}" IsVisibleChanged="TabControl_IsVisibleChanged">
        <local:WorkspaceDeploymentView/>
    </TabItem>
    <TabItem Header="View 3" x:Name="_view3Tab" Visibility="{Binding TabVisibility}" IsVisibleChanged="TabControl_IsVisibleChanged">
        <local:TabDeploymentView/>
    </TabItem>
</TabControl>

这是我背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace MyNameSpace
{                                               
/// <summary>
/// Interaction logic for TopLevelControl.xaml
/// </summary>
public partial class TopLevelControl : UserControl
{
        ApplicationViewModel _viewModel;
        public TopLevelControl()
        {
            _viewModel = new ApplicationViewModel();
            base.DataContext = _viewModel;
            InitializeComponent();
        }

        private void TabControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
           TabItem tab = sender as TabItem;
           if(tab != null && (bool)e.NewValue)
           {
              _tabControl.SelectedIndex = 0;
           }
        }
     }
 }

是否有某些原因导致事件无法解雇?

1 个答案:

答案 0 :(得分:1)

让我们以MVVM的方式做到这一点,没有任何代码隐藏

  

xaml这里我们绑定了SelectedIndex =&#34; {Binding TabSelectedIndex}&#34; TabControl的内容

<TabControl SelectedIndex="{Binding TabSelectedIndex}">
        <TabItem Header="abc">
            <Button Content="ok"/>
        </TabItem>
        <TabItem Header="xyz" Visibility="{Binding TabVisibility}">
            <Button Content="ok"/>
        </TabItem>
        <TabItem Header="pqr" Visibility="{Binding TabVisibility}">
            <Button Content="ok"/>
        </TabItem>
    </TabControl>
  

xaml.cs

public MainWindow()
    {
        InitializeComponent();
        DataContext =new  ViewModel();
    }
  

视图模型

    public class ViewModel: INotifyPropertyChanged
{
    int tabSelectedIndex;
    //this will be bound to the SelectedIndex of Tabcontrol
    public int TabSelectedIndex
    {
        get { return tabSelectedIndex; }
        set { tabSelectedIndex = value;
            Notify("TabSelectedIndex"); 
        }
    }

    Visibility tabVisibility;
    //this will be binded to the Visibility of TabItem 
    public Visibility  TabVisibility
    {
        get { return tabVisibility; }
        set
        {
            tabVisibility = value;
            //this is the logic that will set firstTab selected when Visibility will be collapsed
        if (tabVisibility == Visibility.Collapsed)
        {
            tabSelectedIndex = 0;
            Notify("TabSelectedIndex");
        }
            Notify("TabVisibility"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

我希望这会有所帮助。如果它有帮助那么说MVVM Rocks:)