我有一个带有两个选项卡的窗口,其中包含两个不同的用户控件。为了启用/禁用导航到第二个选项卡,我IsEnabled
在两个 VM 的IPageViewModel
界面中实现了一个属性。
当通过 Messenger 服务从SelectedCustomer
接收到a 时,IsEnabled 布尔属性设置为 true 。CustomerOrdersViewModel
CustomerDetailsViewModel
到目前为止,此方法有效,因为当我从第一个视图的数据网格中选择客户时,第二个选项卡已启用。但问题是当我尝试选择第一个选项卡以返回初始视图时,它已禁用。
这是特定导航问题的屏幕截图。
我不确定为什么当我使用信使将 IsEnabled 属性设置为 true 时,两个选项卡都会启用。
有人对这里的问题有任何建议吗?
在CustomerDetailsViewModel
我通过信使发送 selectedCustomer 中:
private CustomerModel selectedCustomer;
public CustomerModel SelectedCustomer
{
get
{
return selectedCustomer;
}
set
{
selectedCustomer = value;
Messenger.Default.Send<CustomerModel>(selectedCustomer);
RaisePropertyChanged("SelectedCustomer");
}
}
然后在CustomerDetailsViewModel
IsEnabled 属性中设置为 true,因为 SelectedCustomer 已通过:
public CustomerOrdersViewModel()
{
Messenger.Default.Register<CustomerModel>(this, OnCustomerReceived);
}
public void OnCustomerReceived(CustomerModel customer)
{
SelectedCustomer = customer;
IsEnabled = true;
}
这是包含两个用户控件以及为每个控件生成的选项卡的 ApplicationView xaml:
<Window x:Class="MongoDBApp.Views.ApplicationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:MongoDBApp.Views"
xmlns:vm="clr-namespace:MongoDBApp.ViewModels"
Title="ApplicationView"
Width="800"
Height="500">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:CustomerDetailsViewModel}">
<views:CustomerDetailsView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:CustomerOrdersViewModel}">
<views:CustomerOrdersView />
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<vm:ApplicationViewModel />
</Window.DataContext>
<TabControl ItemsSource="{Binding PageViewModels}"
SelectedItem="{Binding CurrentPageViewModel}"
TabStripPlacement="Top">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</Window>