2016-10-11 4 views
0

私はViewModelViewからボタンにアクセスしようとしていますが、私はエラーが出るように私は何かが欠けています:アクセスボタン

Severity Code Description Project File Line Suppression State 
Error CS1061 'MainWindow' does not contain a definition for 'Loadfile' and no extension method 'Loadfile' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?) Uml-Creator C:\Users\HH\Source\Repos\UMLEditor\Uml-Creator\Uml-Creator\View\MainWindow.xaml 54 Active 

ボタンの目的を開くことですがOpenFileDialog

class Load 
    { 

     private void Loadfile(object sender, EventArgs e) 
     { 
      OpenFileDialog loadfile = new OpenFileDialog(); 
      if (loadfile.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      { 
       // File.Text = File.ReadAllText(loadfile.FileName); 
      } 
     } 
} 

とビュー:私のViewModelで私はこのようなクリックを扱う

public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
    } 

EDIT:

<Button x:Name="openButton" ToolTip="Open project" Click="Load_Click"> 
        <Image Source="pack://application:,,,/Images\Open.png" Stretch="UniformToFill" Height="17"></Image> 
       </Button> 
+0

「xaml」はどのように定義されていますか? –

+1

MVVMの概念に違反しています。あなたのビューモデルはあなたの意見について何も知ってはいけません。 viewmodelで動作させたい場合は、ICommand – Alex

+0

を使うべきです。ViewのDataContextが 'Load'クラスに設定されていないようです。 – Rabban

答えて

3

アーキテクチャMVVMでは、ビューとビューモデルが疎結合されています。 XAMLでは、この

public MainWindow() 
{ 
    InitializeComponent(); 
    DataContext = new Load(); 
} 

がプリズムのパッケージを取得し、負荷クラスでは、DelegateCommandなどを使用する場合に使用Nuget

<Button .... Click = "{Binding ClickCommand}" /> 

ような何かを行うようにあなたはViewModelにのインスタンスとして DelegateCommandのようなコマンドを使用して、ビューの DataContextを設定する必要があります

public Load 
{  
    public DelegateCommand<object> _clickCommand; 
    public DelegateCommand<object> ClickCommand  
    { 
     get 
     { 
      if (_clickCommand == null) 
       _clickCommand = new DelegateCommand<object>(OnClickCommandRaised); 
      return _clickCommand; 
     } 
    } 

    public void OnClickCommandRaised(object obj) 
    { 
     //Your click logic. 
    } 
}