MVVM - Button命令绑定不起作用

时间:2018-02-20 19:05:07

标签: c# wpf mvvm binding

我正在使用MVVM模式处理WPF应用程序。 但是,我遇到了麻烦,因为这是我第一次尝试使用MVVM模式。

我有一个主窗口视图和2个UserControls,我想用Buttons切换。很简单,我坚持使用以下代码,这应该可以工作,但是当我点击按钮时没有任何反应:

MainView.xaml

<Window x:Class="WindowsClient.View.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:WindowsClient.ViewModel"
    xmlns:local="clr-namespace:WindowsClient.View"
    Height="768" Width="1366" MinHeight="768" MinWidth="1366">
<Window.Resources>
    <DataTemplate DataType="{x:Type vm:HomeViewModel}">
        <local:HomeView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type vm:ProfilViewModel}">
        <local:ProfilView/>
    </DataTemplate>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="240"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <!-- LeftMenu -->
    <Border Grid.Column="0" Background="Black">
        <StackPanel>
            <Button Content="Home" Command="{Binding HomeViewCommand}"></Button>
            <Button Content="Profil" Command="{Binding ProfilViewCommand}"></Button>
        </StackPanel>
    </Border>
    <!-- Body -->
    <ContentControl x:Name="Pages" Content="{Binding ContentControlViewModel}" Grid.Column="1"/>
</Grid>

MainViewModel.cs

namespace WindowsClient.ViewModel
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private object contentControlViewModel;
        public object ContentControlViewModel
        {
            get => contentControlViewModel;
            set
            {
                contentControlViewModel = value;
                OnPropertyChanged("ContentControlViewModel");
            }
        }

        public MainWindowViewModel()
        {
            HomeViewCommand = new BaseCommand(OpenHome);
            ProfilViewCommand = new BaseCommand(OpenProfil);

        }
        public ICommand HomeViewCommand { get; set; }

        public ICommand ProfilViewCommand { get; set; }

        public void OpenHome(object obj)
        {
            ContentControlViewModel = new HomeViewModel();
        }

        public void OpenProfil(object obj)
        {
            ContentControlViewModel = new ProfilViewModel();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
    }

    public class BaseCommand : ICommand
    {
        private Predicate<object> _canExecute;
        private Action<object> _method;
        public event EventHandler CanExecuteChanged;

        public BaseCommand(Action<object> method)
            : this(method, null)
        {
        }

        public BaseCommand(Action<object> method, Predicate<object> canExecute)
        {
            _method = method;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecute == null)
            {
                return true;
            }

            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _method.Invoke(parameter);
        }
    }
}

HomeView.xaml

<UserControl x:Class="WindowsClient.View.HomeView"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Height="768" Width="1126"
             MinHeight="768" MinWidth="1126">
    <Grid>
        <ToggleButton x:Name="CommunityButton" Content="Community" Height="30" Width="200" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="-300,100,0,0" FontSize="18" />
        <ToggleButton x:Name="NewsButton" Content="News" Height="30" Width="200" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="300,100,0,0" FontSize="18"/>
        <ScrollViewer x:Name="HomeContentViewer" Margin="10,150,10,10"/>
    </Grid>
</UserControl>

HomeViewModel.cs

namespace WindowsClient.ViewModel
{
    internal class HomeViewModel
    {

    }
}

ProfilView和ProfilViewModel与这两者几乎相同。

无论如何,当我点击一个按钮时,视图不会改变,我无法理解为什么......

1 个答案:

答案 0 :(得分:3)

您的MainView.xaml缺少此位:

<Window.DataContext>
    <local:MainWindowViewModel/>
</Window.DataContext>

您可以将其添加到<Window.Resources>行之上。

这就是为什么你的视图模型没有任何东西与你的视图绑定。