Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 内联数据上下文不工作_C#_Wpf_Xaml_Mvvm - Fatal编程技术网

C# 内联数据上下文不工作

C# 内联数据上下文不工作,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我的整个应用程序在单视图模型下工作得非常好。但是,如果我希望使用“Help”按钮的内联Datacontext属性处理另一个ViewModel“VehicleholpViewModel”,则它不起作用。 在VehicleForm.xaml的后端,datacontext设置为VehicleMainViewModel。 但仅对于“帮助”按钮,我想将DataContext更改为VehicleHelpViewModel,并且仅使用内联属性 为什么我的代码不起作用 VehicleForm.xaml Vehi

我的整个应用程序在单视图模型下工作得非常好。但是,如果我希望使用“Help”按钮的内联Datacontext属性处理另一个ViewModel“VehicleholpViewModel”,则它不起作用。 在VehicleForm.xaml的后端,datacontext设置为VehicleMainViewModel。 但仅对于“帮助”按钮,我想将DataContext更改为VehicleHelpViewModel,并且仅使用内联属性

为什么我的代码不起作用

VehicleForm.xaml

VehicleForm.xaml.cs

车辆视图模型

如果VehicleHelpViewModel是一个单独的类,而不是VehicleMainViewModel中的属性,则可以这样重写

<Button Name="Help" Visibility="{Binding HelpVisibility, UpdateSourceTrigger=PropertyChanged}" Command="{Binding OpenHelpWindow_Command}" Height="50" Width="50" HorizontalAlignment="Right">
    <Button.DataContext>
        <l:VehicleHelpViewModel />
    </Button.DataContext>
    <Image Height="45" Width="45" Source="../Images/help.jpg" HorizontalAlignment="Left"/>
</Button>


事实上,更好的名称是使用vm而不是示例中使用的l。按钮上的绑定。DataContext与您称之为“表单”的其余部分处于同一级别,这对于WPF来说是相当不合适的。因此,要使其工作,必须在具有EditText、ListItems和SaveButton_命令属性的同一类中具有名为VehicleHelpViewModel的属性。如果你真的有那笔财产,那么,也许您只需要确保对象已初始化?@Sheridan:您能解释一下您的第一行吗?您的VehicleMainViewModel类中必须有一个名为VehicleHelpViewModel的属性,类型为VehicleHelpViewModel,这样绑定路径才能工作。如果VehicleHelpViewModel作为公共属性存在于VehicleMainViewModel中,则应该按照预期工作,我假设不是这样,您可以通过在xaml中创建一个新的VehicleHelpViewModel实例来实现,正如answerHey pushraj:如何在我的案例中添加名称空间?谢谢!我只是想让我的照片更专业一些:
using Seris.ViewModels;
using Seris.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Seris
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class VehicleForm : Window
{
    public VehicleForm()
    {
        this.DataContext = new VehicleMainViewModel();
        this.Show();
        InitializeComponent();
    }

}
}
using Seris.Commands;
using Seris.Models;
using Seris.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Seris.ViewModels
{
public class VehicleHelpViewModel : ObservableObject
{
    private ICommand _OpenHelpWindow_Command;
    public ICommand OpenHelpWindow_Command
    {
        get { return _OpenHelpWindow_Command; }
        set { _OpenHelpWindow_Command = value; }
    }
    public void OpenHelp(object o1)
    {
        Help help = new Help();
        help.Show();
    }
    public VehicleHelpViewModel()
    {
        OpenHelpWindow_Command = new RelayCommand(new Action<object>(OpenHelp));
    }
}
}
<Button Name="Help" Visibility="{Binding HelpVisibility, UpdateSourceTrigger=PropertyChanged}" Command="{Binding OpenHelpWindow_Command}" Height="50" Width="50" HorizontalAlignment="Right">
    <Button.DataContext>
        <l:VehicleHelpViewModel />
    </Button.DataContext>
    <Image Height="45" Width="45" Source="../Images/help.jpg" HorizontalAlignment="Left"/>
</Button>
<Window x:Class="Seris.VehicleForm"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="VehicalForm" Height="500" Width="650"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:l="clr-namespace:Seris.ViewModels" >
<Button Name="Help" Visibility="{Binding HelpVisibility, UpdateSourceTrigger=PropertyChanged}" Command="{Binding OpenHelpWindow_Command}" Height="50" Width="50" HorizontalAlignment="Right"
        xmlns:l="clr-namespace:Seris.ViewModels">