2011-07-25 15 views
0

私はMVVM(WPF)で2つのビューを持っています。最初のビューには2つのテキストボックスがあります。ユーザー名、パスワード、2番目のビューには送信とクリアという2つのボタンがあります。両方のビューがOn Formに設定されました。 'Clear'ボタンを押すと、テキストボックスがクリアされ、SubmitにUserNameとPasswordというメッセージが表示されます。私はMVVM + WPFのみを使用しています。プリズムではありません。WPF MVVMでの2つのビュー間の通信

最初のビューのうちのModelView:

class LoginView:ViewModelBase 
    { 
     string _userName; 
     public string UserName 
     { 
      get {return _userName ; } 
      set { 
       if (_userName != value) 
       { 
        _userName = value; 
       } 
       base.OnPropertyChanged(UserName); 
      } 
     } 

     string _Pwd; 
     public string PWD 
     { 
      get { return _Pwd; } 
      set 
      { 

       _Pwd = value; 
       base.OnPropertyChanged(_Pwd); 
      } 
     } 
    } 

とボタン

class ButtonHandler 
    { 
     private DelegateCommand _ClearData; 
     public ICommand ClearCommand 
     { 
      get 
      { 
       if (_ClearData == null) 
       { 
        _ClearData = new DelegateCommand(ClearText); 
       } 
       return _ClearData; 
      } 
     } 
     LoginView lg = new LoginView(); 
     private void ClearText() 
     { 
      lg.UserName = ""; 
      lg.PWD = ""; 
     } 
    } 

第1の制御

<Label Content="Login" Grid.Row="0" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left" 
      FontFamily="Georgia" FontSize="24" FontWeight="UltraBold" ></Label> 
    <Label Content="User Name" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></Label> 
    <Label Content="Password" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></Label> 
    <TextBox Name="username" Grid.Row="1" Grid.Column="1" Margin="100,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Path=UserName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ></TextBox> 
    <TextBox Name="pwd" Grid.Row="2" Grid.Column="1" Margin="100,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Path=PWD,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox> 
    <Separator Grid.Row="0" Grid.Column="1" Height="5" Margin="0,40,0,0" Background="Green"></Separator> 

とボタンの表示

 <Button x:Name="Submit" Content="Submit" Grid.Column="1"></Button> 
    <Button x:Name="Clear" Content="Clear" Grid.Column="2" 
      Command="{Binding Path=ClearCommand, Mode=OneWay, 
      UpdateSourceTrigger=PropertyChanged}" > 
    </Button> 
のコードの表示のために

なぜ機能しないのですか?

+0

あなたは* *句読点を使用してもらえ

これはあなたの例ではMVVMを利用する一つの方法であるように見えるでしょうか? –

答えて

2

MVVMパターンは正しく使用されていません。このパターンでは、ViewModelにはビューへの参照がありません。コマンドがViewModelの一部であるため、LoginViewへの参照がパターンに違反します。

2つの入力フィールドとボタンがありますか?私は単一のViewModelと1つのViewを持っています。 ViewModelは、2つの文字列プロパティ(ユーザ名&パスワード)とクリアボタンにバインドするコマンドを公開します。コマンドが実行されると、ViewModel上のユーザー名とパスワードのテキストが消去されます。ビューはそれに応じて更新されます。

+0

ColinE実際に私はMMVMの新しいユーザーです。私は2つのビュー間で通信しようとするので、私はこの作業が単一のビューで行われていることを知っています。 – user831555

+0

ちょうどsidenote:ViewModelにビューへの参照が必要なまれな条件があります。これは、たとえば、ViewModel-Firstアプローチが使用されている場合のPRISMの場合です。ここでは、ビューのデータコンテキストを設定するために参照が使用されます(インタフェースを使用して、DataContextを設定する場合にのみビュー非依存結合が使用されます)。しかし、あなたの主なメッセージは正しいです:ViewModelはビューと直接対話してはいけません。 – PVitt

1

MVVMの基本的な原則は、内部にすべてのアプリケーションロジックを持つビューをバインドできるクラスを持つことです。主な理由の1つはa separation of concerns.です。したがって、ユーザー名が必要な場合は、ビューがバインドするプロパティを公開し、ログインするときに、そのバインドされた値を使用してビジネスロジック層に送信する関数を作成します応用。あなたのXAMLで

public class LoginViewModel 
{ 
    public string UserName {get;set;}//Implement INotifyPropertyChanged 
    public string PWD {get;set;} 

    private DelegateCommand _ClearData; 
    public ICommand ClearCommand 
    { 
     get 
     { 
      if (_ClearData == null) 
      { 
       _ClearData = new DelegateCommand(ClearText); 
      } 
      return _ClearData; 
     } 
    } 

    private void ClearText() 
    { 
     UserName = ""; 
     PWD = ""; 
    } 
} 

、その後:

<TextBox Text={Binding UserName} /> 
<TextBox Text={Binding PWD} /> 
<Button Command={Binding ClearCommand}/> 
関連する問題