内联DataContext不起作用

时间:2014-08-06 09:56:34

标签: c# wpf xaml mvvm

我的整个应用程序与单一View-Model完美配合。但是,我希望使用“帮助”按钮的内联Datacontext属性来处理另一个ViewModel“VehicleHelpViewModel”,它无法正常工作。 在VehicleForm.xaml的后端,datacontext设置为VehicleMainViewModel。 但仅针对“帮助”按钮,我想将DataContext更改为VehicleHelpViewModel并仅使用内联属性。

为什么我的代码无效?

VehicleForm.xaml

<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">


<WrapPanel Orientation="Vertical" Margin="10 " >

    <Label Content="Vehical No" HorizontalAlignment="Left"/>
    <TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding VehicleNo, UpdateSourceTrigger=PropertyChanged}"  HorizontalAlignment="Left" />

    <Label Name="VehicleNoError_Label" Foreground="Red" Content="{Binding VehicleNo_Error, UpdateSourceTrigger=PropertyChanged}" Height="36" Width="186"/>

    <Label Content="Model" HorizontalAlignment="Left"/>
    <TextBox Name="Model_Text" Height="24" Width="80" TextWrapping="Wrap" Text="{Binding Model, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" />
    <Label x:Name="ModelError_Label" Foreground="Red" Content="{Binding Model_Error, UpdateSourceTrigger=PropertyChanged}" Height="36" Width="186"/>

    <Label Content="Manufacturing Date" HorizontalAlignment="Left"/>
    <DatePicker Name="ManufacturingDate_DateTime" SelectedDate="{Binding ManufacturingDate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="136"/>
    <Label Name="ManufacturingDateError_Label" Foreground="Red" Content="{Binding ManufacturingDate_Error, UpdateSourceTrigger=PropertyChanged}" Height="36" Width="136"/>

    <Label Content="IU No" HorizontalAlignment="Left"/>
    <TextBox Height="23" Width="80" Name="IUNO_Text" TextWrapping="Wrap" Text="{Binding IUNo, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left"/>
        <Label Name="IUError_Label" Foreground="Red" Content="{Binding IU_Error, UpdateSourceTrigger=PropertyChanged}" Height="36" Width="186"/>

    <Label Content="Personnel" HorizontalAlignment="Left"/>
    <ComboBox x:Name="Personnel_Combo" SelectedValue="{Binding PersonnelNameSelected, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding PersonnelName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Width="126"/>

    <Separator Height="20" RenderTransformOrigin="0.5,0.5" Width="16"/>

    <Button Name="Save_Button" Command="{Binding SaveButton_Command}" Content="Save" Width="66"/>
    <Button Name="Replace_Button" CommandParameter="replace" IsEnabled="{Binding isEnableReplaceButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Command="{Binding ReplaceButton_Command}" Content="Replace" Width="66"/>
    <Button Name="Remove_Button" CommandParameter="replace" IsEnabled="{Binding isEnableReplaceButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Command="{Binding RemoveButton_Command}" Content="Remove" Width="66"/>


    <Label x:Name="Error_Label" Content="{Binding ErrorMessage, UpdateSourceTrigger=PropertyChanged}" Foreground="Red" HorizontalAlignment="Left" Height="41" Width="137"/>

    <ListView Name ="Grid" Height="294" Width="371" >
        <DataGrid Name="DG" ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedRow, Mode=TwoWay}" GridLinesVisibility="None" IsReadOnly="True" AutoGenerateColumns="False" BorderThickness="0">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Vehical No" Binding="{Binding VehicalNo}"/>
                <DataGridTextColumn Header="Model" Binding="{Binding Model}" />
                <DataGridTextColumn Header="ManufacturingDate" Binding="{Binding ManufacturingDate}" />
                <DataGridTextColumn Header="IUNo" Binding="{Binding IUNo}" />
                <DataGridTextColumn Header="Personnel" Binding="{Binding PersonnelNameSelected}" />
                <DataGridTextColumn Header="Unique No" Binding="{Binding UniqueNo}"/>
            </DataGrid.Columns>
        </DataGrid>
    </ListView>

    <TextBlock Name="Preview" Text="{Binding EditText, UpdateSourceTrigger=PropertyChanged}"/>

    <ProgressBar Name="Progressbar" Minimum="0" Maximum="100" Value="{Binding Progress, UpdateSourceTrigger=PropertyChanged}" Height="11"/>
    <TextBlock Text="{Binding ElementName=Progressbar, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center"/>

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

</WrapPanel>

</Window>

VehicleForm.xaml.cs

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();
    }

}
}

VehicleHelpViewModel

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));
    }
}
}

1 个答案:

答案 0 :(得分:0)

如果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>

在上面的示例中l:将引用VehicleHelpViewModel类的名称空间

修改

基于相关编辑,为示例中指定的l:指定正确的命名空间

将以下内容添加到Window元素或按钮元素本身

xmlns:l="clr-namespace:Seris.ViewModels"

例如

<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">

实际上,更好的名称是使用vm代替l,例如