【问题标题】:MVVM - Button command binding not workingMVVM - 按钮命令绑定不起作用
【发布时间】:2018-02-21 04:05:10
【问题描述】:

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

我有一个主窗口视图和 2 个用户控件,我想使用按钮进行切换。虽然很简单,但我坚持使用以下代码,它应该可以工作,但是当我单击按钮时什么也没有发生:

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 与这两个几乎相同。

无论如何,当我点击一个按钮时,视图并没有改变,我不明白为什么......

【问题讨论】:

  • 我假设您正在单击“主页”或“配置文件”按钮,对吧?您是否尝试在OpenHomeOpenProfil 中设置断点以查看您的命令是否正在执行?您的MainView 上的DataContext 是否实际上设置为MainWindowViewModel 实例?
  • 是的。我在OpenHome/OpenProfil 中尝试了断点,但似乎它永远不会到达它。我要四处寻找 DataContext,也许我错过了一些关于它的东西。

标签: c# wpf mvvm binding


【解决方案1】:

您的 MainView.xaml 中缺少此位:

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

您可以将其添加到您的 &lt;Window.Resources&gt; 行上方。

这就是为什么您的视图模型中没有任何内容绑定到您的视图的原因。

【讨论】:

  • 这就是问题所在。谢谢。
猜你喜欢
  • 2020-03-06
  • 2019-05-20
  • 1970-01-01
  • 2015-06-07
  • 2015-12-09
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
相关资源
最近更新 更多