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