0
我試圖從View
在ViewModel
訪問按鈕,但我失去了一些東西,因爲我得到的錯誤:訪問按鈕
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
。在我ViewModel
我處理click這樣的:
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);
}
}
}
和視圖:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
編輯:
<Button x:Name="openButton" ToolTip="Open project" Click="Load_Click">
<Image Source="pack://application:,,,/Images\Open.png" Stretch="UniformToFill" Height="17"></Image>
</Button>
xaml是如何定義的? –
您違反了MVVM的概念。你的viewmodel不應該知道你的視圖。如果你想在你的viewmodel中有行爲,你應該使用ICommand – Alex
看來你的View的DataContext沒有設置爲你的'Load'類。 – Rabban