【发布时间】:2016-10-11 11:05:20
【问题描述】:
我正在尝试从ViewModel 中的View 访问一个按钮,但是当我收到错误消息时我遗漏了一些东西:
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 中,我像这样处理点击:
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 的概念。您的视图模型不应该对您的视图一无所知。如果你想在你的视图模型中有行为,你应该使用 ICommand
-
您的 View 的 DataContext 似乎没有设置为您的
Load类。 -
@NikhilAgrawal 它很大,但我已将按钮放在主窗口中
-
@GalDak:只有你的按钮。
标签: c# wpf mvvm openfiledialog