如何使用数据模板处理多个选项卡的已启用属性? [英] How to handle enabled property of multiple tabs using Data Templates?
问题描述
我有一个带有两个选项卡的窗口,其中包含两个不同的用户控件.为了启用/禁用导航到第二个选项卡,我从IPageViewModel
界面在两个VM中实现了一个IsEnabled
属性.
I have a Window with two tabs, that holds two different user controls. In order to enable/disable navigation to the second tab, I implement an IsEnabled
property in both VM's from the IPageViewModel
interface.
当通过CustomerDetailsViewModel
的Messenger服务在CustomerOrdersViewModel
中接收到SelectedCustomer
时,IsEnabled布尔值属性设置为true.
The IsEnabled boolean property is set to true when a SelectedCustomer
is received in the CustomerOrdersViewModel
, via Messenger service from CustomerDetailsViewModel
.
到目前为止,此方法有效,因为当我从第一个视图的数据网格中选择一个客户时,第二个选项卡已启用.但是问题是当我尝试选择第一个选项卡以返回初始视图时,它被禁用.
So far this method works, as the second tab is enabled when I select a customer from the data grid in the first view. But the problem is when I try to select the first tab to go back to the initial view, it is disabled.
这是特定导航问题的屏幕截图.
This is a screen cast of the specific navigation issue.
我不确定为什么使用信使将IsEnabled属性设置为true时,两个选项卡都将启用.
I'm not sure why as I thought when I set the IsEnabled property to true using the messenger, both tabs would be enabled.
有人在此问题上有任何建议吗?
Does anyone have any advice on the issue here?
在CustomerDetailsViewModel
中,我通过Messenger发送selectedCustomer:
In the CustomerDetailsViewModel
I send the selectedCustomer via a messenger:
private CustomerModel selectedCustomer;
public CustomerModel SelectedCustomer
{
get
{
return selectedCustomer;
}
set
{
selectedCustomer = value;
Messenger.Default.Send<CustomerModel>(selectedCustomer);
RaisePropertyChanged("SelectedCustomer");
}
}
然后在CustomerDetailsViewModel
中将IsEnabled属性设置为true,因为已传递SelectedCustomer:
Then in the CustomerDetailsViewModel
the IsEnabled property is set to true as the SelectedCustomer has been passed over:
public CustomerOrdersViewModel()
{
Messenger.Default.Register<CustomerModel>(this, OnCustomerReceived);
}
public void OnCustomerReceived(CustomerModel customer)
{
SelectedCustomer = customer;
IsEnabled = true;
}
这是既包含用户控件又包含为每个控件生成的选项卡的ApplicationView xaml:
This is the ApplicationView xaml that holds both user controls, and the tabs generated for each:
<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?
Why not just default the IsEnabled
property of the CustomerDetailsViewModel
to true?
该标签应始终启用,这样对我来说最有意义.
It's a tab that should always be enabled, so that would make the most sense to me.
这篇关于如何使用数据模板处理多个选项卡的已启用属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!