Binding 通过命令绑定时出现问题。您能提供帮助吗

Binding 通过命令绑定时出现问题。您能提供帮助吗,binding,Binding,新的wpf和通过学习曲线。 我有一个带有工具栏保存按钮和文本框的用户控件。 我正在努力实现的目标如下 当我按下工具栏中的保存按钮时,我应该在文本框中记录我将要保存的内容以及我已保存的客户(CustomerView UserControl) 我似乎有两个问题 1) SaveCommand没有被钩住我以为我已经钩住了 2) 未将操作写入文本框 你能告诉我哪里出了问题吗? 非常感谢 MainWindow.xaml <Window x:Class="MyCompany.CustomerStore.

新的wpf和通过学习曲线。 我有一个带有工具栏保存按钮和文本框的用户控件。 我正在努力实现的目标如下

当我按下工具栏中的保存按钮时,我应该在文本框中记录我将要保存的内容以及我已保存的客户(CustomerView UserControl) 我似乎有两个问题

1) SaveCommand没有被钩住我以为我已经钩住了

2) 未将操作写入文本框

你能告诉我哪里出了问题吗? 非常感谢

MainWindow.xaml

<Window x:Class="MyCompany.CustomerStore.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:view="clr-namespace:MyCompany.CustomerStore.Views"
      Title="MainWindow" Height="350" Width="525">
   <Grid>
       <view:CustomerView></view:CustomerView>
   </Grid>

在何处声明视图之间的关系

x:Class=“MyCompany.CustomerStore.Views.CustomerView

模型类CustomerViewModel呢

我哪儿都看不到


我认为您需要将视图的DataContext设置为模型。

很抱歉回复太晚。是的,DataContext是问题所在,谢谢您指出。
  CustomerView.xaml


    <UserControl x:Class="MyCompany.CustomerStore.Views.CustomerView"
          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">
   <Grid>
       <DockPanel LastChildFill="True">
           <ToolBar DockPanel.Dock="Top">
               <Button Command="{Binding Path=SaveCommand}">Save</Button>
           </ToolBar>
           <TextBox Name="txtPrintAction" Text="{Binding CustomerLog, Mode=TwoWay}"></TextBox>
       </DockPanel>
   </Grid>
  CustomerModel.cs


 public class CustomerModel
  {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string CustomerLog { get; set; }
  }

  CustomerViewModel.cs

  public class CustomerViewModel:WorkspaceViewModel,ICustomerViewModel
  {
      readonly CustomerModel _customerModel;

      RelayCommand _saveCommand;

      public CustomerViewModel(CustomerModel customer)
      {
          _customerModel = customer;
      }
      public string FirstName
      {
          get { return _customerModel.FirstName; }
          set
          {                 
              _customerModel.FirstName = value;
              base.OnPropertyChanged("FirstName");
          }
      }
      public string LastName
      {
          get { return _customerModel.LastName; }
          set
          {
              _customerModel.LastName = value;
              base.OnPropertyChanged("LastName");
          }
      }
      public string CustomerLog
      {
          get { return _customerModel.CustomerLog; }
          set
          {
              _customerModel.CustomerLog = value;
              base.OnPropertyChanged("CustomerLog");
          }
      }
      public ICommand SaveCommand
      {
          get
          {
              if (_saveCommand == null)
              {
                  _saveCommand = new RelayCommand(param => Save(), param => CanSave);
              }
              return _saveCommand;
          }
      }

      private  void Save()
      {
          AppendToLog("I am about to save");

          //Pretend we have saved the customer

          AppendToLog("CustomerSaved");
      }

      internal void AppendToLog(string text)
      {
          _customerModel.CustomerLog += text + Environment.NewLine; ;
          OnPropertyChanged("CustomerLog");
      }

      static bool CanSave
      {
          get
          {
              return true; 
          } 
      }