2010-02-21 3 views
0

wpf 및 학습 과정을 처음 사용. 툴바 저장 버튼과 TextBox가있는 userControl이 있습니다. 내가 내가 내가 저장하려고 오전 나는 것 같다 고객 (CustomerView UserControl을) 을 저장하는 텍스트 상자에 기록해야 도구 모음에서 저장 버튼을 누르면문제 명령을 통해 바인딩.

을 다음과 같이 내가 달성하기 위해 시도하고있다 SaveCommand 2 문제

1

)에 중독되지 않은 것을 나는 그것을

2) 텍스트 상자에 조치를 작성되지 구부려 생각했다.

내가 어디로 잘못 가고 있는지 말해 줄 수 있습니까? 감사합니다. !!!

MainWindow.xaml 당신이보기 사이의 관계

X 선언 할

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

답변

0

: 클래스 = "MyCompany.CustomerStore은. Views.Customer

및 모델 클래스 CustomerViewModel?

어디에도 표시되지 않습니다.

당신은 모델의 뷰의 DataContext를 설정해야한다고 생각합니다.

+0

늦게 답장을 보내서 죄송합니다. 데이터 콘 텍스는 문제를 지적 해 주셔서 감사합니다. – user9969