1

我有一个带有两个选项卡的窗口,其中包含两个不同的用户控件。为了启用/禁用导航到第二个选项卡,我IsEnabled在两个 VM 的IPageViewModel界面中实现了一个属性。

当通过 Messenger 服务从SelectedCustomer接收到a 时,IsEnabled 布尔属性设置为 true 。CustomerOrdersViewModelCustomerDetailsViewModel

到目前为止,此方法有效,因为当我从第一个视图的数据网格中选择客户时,第二个选项卡已启用。但问题是当我尝试选择第一个选项卡以返回初始视图时,它已禁用

这是特定导航问题的屏幕截图。

我不确定为什么当我使用信使将 IsEnabled 属性设置为 true 时,两个选项卡都会启用。

有人对这里的问题有任何建议吗?

CustomerDetailsViewModel我通过信使发送 selectedCustomer 中:

    private CustomerModel selectedCustomer;
    public CustomerModel SelectedCustomer
    {
        get
        {
            return selectedCustomer;
        }
        set
        {
            selectedCustomer = value;
            Messenger.Default.Send<CustomerModel>(selectedCustomer);
            RaisePropertyChanged("SelectedCustomer");
        }
    }

然后在CustomerDetailsViewModelIsEnabled 属性中设置为 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>
4

2 回答 2

1

我假设您将新的 ViewModel(CustomerDetails 或CustomerOrders)分配给 CurrentPageViewModel。每当您这样做时,都会创建一个新的类对象,默认情况下IsEnabled设置为 false。
解决方法是在与您的 View (ApplicationViewModel) 关联的 ViewModel 中创建IsEnabled属性。然后在 ItemContrainerStyle 中引用它如下:

 <Setter Property="IsEnabled" Value="{Binding RelativeSource={RelativeSource AncestoryType=Window}, Path=DataContext.IsEnabled}"/>

TabControlPages 的 ViewModel 中既不需要IsEnabled也不需要Messages ,因为您的IsEnabled属性位于主 ViewModel 中,并且两个 TabPages 都引用它。
编辑
过了一会儿,我意识到它会默认禁用,因为 IsDefault 一开始就等于 false。这很复杂,因为您没有显式创建 TabPage。我为此附上了完整的解决方案,请看 这里

于 2015-12-02T12:56:59.303 回答
1

为什么不直接将 的IsEnabled属性默认CustomerDetailsViewModel为 true 呢?

这是一个应该始终启用的选项卡,所以这对我来说最有意义。

于 2015-12-02T16:07:21.717 回答