gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:22:08 24 4
gpt4 key购买 nike

我必须通过从 WPF MVVM 应用程序(c#)中的第一个选项卡单击按钮导航到另一个选项卡。
我试图通过在选项卡控件中添加对选定索引属性的绑定(bind)来实现这一点。第一个选项卡中使用了两种不同的 View 模型。在选项卡控件中添加对选定索引属性的绑定(bind)后,它失去了 View 模型的其余部分的访问权限并且否数据显示在第一个选项卡的文本框中。导航也不起作用。如果窗口有多个 View 模型,我如何使用选项卡导航。请参阅示例代码。
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;
}
在为选定索引添加绑定(bind)之前没有问题。

最佳答案

您的问题是您正在设置 TabControl.DataContext . DataContext是继承的,所以它里面的所有控件现在都使用processVM作为它们的绑定(bind)源而不是 MainWindowViewModel .
而不是设置 TabControl.DataContext ,更改SelectedIndex绑定(bind)到这个:

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

关于c# - 通过从具有多个 View 模型的选项卡单击按钮导航到另一个选项卡项不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62721703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com