通过从具有多个视图模型的选项卡单击按钮导航到另一个选项卡项不起作用

Navigate to another tab item by a button click from the tab having multiple view models doesn't work

我必须通过从 WPF MVVM 应用程序 (c#) 中的第一个选项卡单击按钮导航到另一个选项卡。 我试图通过在选项卡 control.There 中添加对 Selected Index 属性 的绑定来实现此目的是在第一个 tab.After 中使用两个不同的视图模型添加对 Selected Index 属性 的绑定选项卡控件它失去了视图模型的其余访问权限,并且第一个选项卡中的文本框上没有数据。导航也不起作用。如果 window 有多个视图模型,我如何使用选项卡导航。请查看示例代码。

XAML 文件

MainWindow.xaml

  <Grid>
    <TabControl SelectedIndex="{Binding SelectedTab,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                 DataContext="{Binding processVM}">
        <TabItem Header="Home">
            <Grid ShowGridLines="false" >
                <Grid.ColumnDefinitions >
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                </Grid.RowDefinitions>
                <TextBox Name="txtCustomerName" 
                         Grid.Row="0" Grid.Column="1"
                         Text="{Binding CustomerName}"
                       DataContext="{Binding customerVM}"></TextBox>
                <TextBox Name="txtDepositAmount" 
                         Grid.Row="1" Grid.Column="1"
                         Text="{Binding DepositAmount}"
                       DataContext="{Binding customerVM}"></TextBox>
                <Button Content="Click"  Width="100" Height="50"
                        Grid.Row="2" 
                        DataContext="{Binding processVM}"
                        Command="{Binding ButtonCommand}"                             
                        />
            </Grid>

代码隐藏

MainWindow.xaml.cs

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel()
            {
                processVM = new ProcessViewModel(),
                customerVM = new CustomerViewModel()
            };
        }
    }

查看模型

MainWindowViewModel.cs

  class MainWindowViewModel
    {
        public ProcessViewModel processVM { get; set; }
        public CustomerViewModel customerVM { get; set; }
    }

ProcessViewModel.cs

 public class ProcessViewModel: INotifyPropertyChanged
    {
        private string depositAmount;

        public string DepositAmount
        {
            get { return depositAmount; }
            set {
                depositAmount = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DepositAmount"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private ICommand m_ButtonCommand;
        public ICommand ButtonCommand
        {
            get
            {
                return m_ButtonCommand;
            }
            set
            {
                m_ButtonCommand = value;
            }
        }

        private int selectedTab;

     

        public int SelectedTab
        {
            get { return selectedTab; }
            set
            {
                selectedTab = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedTab"));
            }
        }


        public ProcessViewModel()
        {
            ButtonCommand = new RelayCommand(new Action<object>(clickbutton));
            depositAmount = "450";
        }

        public void clickbutton(object obj)
        {
            MessageBox.Show("clicked");
            SelectedTab = 1;
        }
    }

CustomerViewModel.cs

 class CustomerViewModel: ProcessViewModel, INotifyPropertyChanged
    {
        private string customerName;

        public string CustomerName
        {
            get { return customerName; }
            set { customerName = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CustomerName"));
            }
        }
        public CustomerViewModel()
        {
            CustomerName = "Alex";
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

在为所选索引添加绑定之前没有任何问题。

您的问题是您正在设置 TabControl.DataContextDataContext 是继承的,所以它里面的所有控件现在都使用 processVM 作为它们的绑定源而不是 MainWindowViewModel

而不是设置 TabControl.DataContext,将 SelectedIndex 绑定更改为此:

SelectedIndex="{Binding processVM.SelectedTab, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"