在 App.xaml.cs 之外的 WPF 中设置 DataContext

问题描述 投票:0回答:1

我有一个

MainWindow
,显示
LoginView
SignUpView
StoreView
DataContext
在这里工作正常。

StoreView
内,它应该显示几个其他视图,默认为
HomeView

鉴于我使用命令和 MVVM,如何设置 DataContext

店铺视图:

<UserControl x:Class="OBOS.Views.StoreView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:OBOS.Views"
             xmlns:viewModels="clr-namespace:OBOS.ViewModels"
             mc:Ignorable="d" 
             Height="640" Width="1200">
    <Border Background="AntiqueWhite"
            CornerRadius="20">
        <Grid>

            <Grid.RowDefinitions>
                <RowDefinition Height="100"/>
                <RowDefinition Height="60"/>
                <RowDefinition />
                <RowDefinition Height="20"/>
            </Grid.RowDefinitions>

            
            <Grid Grid.Row="2">
                <Grid.Resources>
                    <DataTemplate DataType="{x:Type viewModels:HomeViewModel}">
                        <local:HomeView/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type viewModels:CartViewModel}">
                        <local:CartView/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type viewModels:HistoryViewModel}">
                        <local:HistoryView/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type viewModels:SearchViewModel}">
                        <local:SearchView/>
                    </DataTemplate>
                </Grid.Resources>
                <ContentControl Content="{Binding StoreCurrentViewModel}"/>
            </Grid>

        </Grid>
    </Border>
</UserControl>

商店视图模型:

public class StoreViewModel : ViewModelBase
    {
        private readonly NavigationStore _storeNavigationStore;
        public ViewModelBase StoreCurrentViewModel => _storeNavigationStore.CurrentViewModel;


        public ICommand ToLogin { get; }
        public ICommand ToHistory { get; }
        public ICommand ToCart { get; }
        public ICommand ToHome { get; }
        public ICommand SearchCommand { get; }

        public StoreViewModel(NavigationStore navigationStore)
        {
            _storeNavigationStore = new NavigationStore();
            _storeNavigationStore.CurrentViewModel = new HomeViewModel(_storeNavigationStore);

            _storeNavigationStore.CurrentViewModelChanged += OnCurrentViewModelChanged;

            ToLogin = new ToLogin(navigationStore);
            ToHistory = new ToHistory(_storeNavigationStore);
            ToCart = new ToCart(_storeNavigationStore);
            SearchCommand = new SearchCommand(_storeNavigationStore);
            ToHome = new ToHome(_storeNavigationStore);
        }

        private void OnCurrentViewModelChanged()
        {
            OnPropertyChanged(nameof(StoreCurrentViewModel));
        }

    }

这是 HomeCommand:

public class ToHome : CommandBase
    {
        private readonly NavigationStore _navigationStore;

        public ToHome(NavigationStore navigationStore)
        {
            _navigationStore = navigationStore;
        }

        public override void Execute(object parameter)
        {
            _navigationStore.CurrentViewModel = new HomeViewModel(_navigationStore);
        }
    }

所有命令的行为方式都与此命令相同。

现在,StoreView 显示的所有视图都缺少 DataContext,我该如何解决这个问题?

c# wpf mvvm
1个回答
0
投票

您只需将

StoreCurrentViewModel
StoreViewModel
属性设置为
CartViewModel
/
HistoryViewModel
/
SearchViewModel
即可显示
CartViewM
/
HistoryView
/
SearchView

视图将自动从

DataContext
Content
属性继承其
ContentControl
。您不应明确设置
DataContext

© www.soinside.com 2019 - 2024. All rights reserved.