【发布时间】:2015-12-02 12:16:25
【问题描述】:
我有一个带有两个选项卡的窗口,其中包含两个不同的用户控件。为了启用/禁用到第二个选项卡的导航,我通过IPageViewModel 接口在两个VM 中实现了IsEnabled 属性。
当通过来自CustomerDetailsViewModel 的 Messenger 服务在 CustomerOrdersViewModel 中接收到 SelectedCustomer 时,IsEnabled 布尔属性设置为 true。
到目前为止,此方法有效,因为当我从第一个视图的数据网格中选择客户时,启用了第二个选项卡。但问题是当我尝试选择第一个选项卡以返回初始视图时,它被 禁用。
这是具体导航issue的截屏。
我不知道为什么当我使用 Messenger 将 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>
【问题讨论】:
-
为什么不将
CustomerDetailsViewModel的IsEnabled属性默认为true?这是一个应该始终启用的选项卡,所以这对我来说最有意义。 -
好的,IsEnabled 属性对于每个 VM 是独立的吗?那么这将是有道理的。
-
是的,每个 IPageViewModel 都是每个选项卡的数据对象,因此它们可以有单独的
IsEnabled值:)
标签: wpf mvvm tabcontrol boolean-logic itemcontainerstyle