2010-02-21 3 views
0

Nouveau à wpf et à travers une courbe d'apprentissage. J'ai un userControl avec un bouton Save de la barre d'outils et un TextBox. Ce que je suis en train de réaliser est la suivanteProblème Liaison par commande.Pouvez-vous aider

Lorsque j'appuie sur le bouton Enregistrer dans la barre d'outils, je dois enregistrer dans la zone de texte que je suis sur le point d'enregistrer et que je l'ai sauvé le client (CustomerView UserControl) me semble ont 2 problèmes

1) que le SaveCommand n'est pas accroché, je pensais que je l'avais accroché il

2) n'est pas en train d'écrire l'action à la zone de texte.

Pourriez-vous me dire où je me trompe? Merci beaucoup !!!

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> 

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; 
      } 
     } 

Répondre

0

Où déclarez-vous une relation entre la vue

x: Class = "MyCompany.CustomerStore. Views.Customer Voir

et la classe de modèle CustomerViewModel?

Je ne vois ça nulle part.

Je pense que vous devez définir le DataContext de la vue au modèle.

+0

désolé pour la réponse tardive.Yes DataContext a été le problème merci de le signaler. – user9969