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

标签 c# wpf xaml user-controls

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

选项卡的可见性绑定(bind)到我的 ViewModel,并以这种方式更新。

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

这是我的 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;
           }
        }
     }
 }

事件没有触发是否有某种原因?

最佳答案

让我们在没有任何代码隐藏的情况下以 MVVM 方式完成

xaml here we have binded SelectedIndex="{Binding TabSelectedIndex}" of 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();
    }

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 :)

关于c# - 当以编程方式更新 TabItem 可见性时,不会触发 IsVisibleChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926006/

相关文章:

c# - 如何在我的应用程序中集成 Visual Studio

wpf - 我可以在WPF MVVM应用程序中使用App Domains来缩短启动时间,并且仅在需要时才加载程序集吗?

c# - WPF:动态过滤数据网格

c# - 如何更改 Windows Phone 中同一个 ListBox 中另一个对象的可见性?

wpf - 创建一个 WPF 文本框控件,如 Outlook 电子邮件收件人文本框

c# - 在 C# 中将文本转换为 bool 表达式

c# - 创建抽象类的对象。怎么可能

c# - Xamarin.Forms Shell 在视觉层次结构之外导航

c# - Json.net 在 Web API 中使用,但在 Razor 模板中调用 Json.Encode 时不使用

wpf - DependencyProperty.RegisterAttached 和多个实例