如何使用MVVM在命令中访问视图模型对象

时间:2012-12-03 09:42:10

标签: wpf mvvm

您好我是MVVM的新手。

我的XAML如下

<Window.DataContext>
    <HelloViewModel:EmpDetailsWindowViewModel/>
</Window.DataContext>
<Grid>
    <igDP:XamDataGrid  Name="dgEmployee" DataSource="{Binding EmployeeDataTable, Mode=TwoWay}" ScrollingMode="DeferredWithScrollTips" 
                       RecordContainerGenerationMode="Virtualize"  ScrollBehaviorOnListChange="PreserveRecordsInView" FieldLayoutInitialized="dgEmployee_FieldLayoutInitialized">
        <igDP:XamDataGrid.ViewSettings>
            <igDP:GridViewSettings />
        </igDP:XamDataGrid.ViewSettings>
        <igDP:XamDataGrid.FieldLayoutSettings >
            <igDP:FieldLayoutSettings AutoGenerateFields="True" />
        </igDP:XamDataGrid.FieldLayoutSettings>
        <igDP:XamDataGrid.FieldLayouts>
            <igDP:FieldLayout/>
        </igDP:XamDataGrid.FieldLayouts>
        <igDP:XamDataGrid.FieldSettings>
            <igDP:FieldSettings AllowRecordFiltering="True" AllowSummaries="True" AllowEdit="True" AutoSizeScope="ViewableRecords" />
        </igDP:XamDataGrid.FieldSettings>
    </igDP:XamDataGrid>
    <Button x:Name="btnSubmit" Content="Submit" Command="{Binding SubmitCommand}"  CommandParameter="submit command" HorizontalAlignment="Left" Margin="212,215,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>

我想在SubmitCommand的Execute方法中访问viewmodel“EmployeeDataTable”的属性

 public void Execute(object parameter)
        {
            //here

            MessageBox.Show("you are safe !!1");
        }

我该怎么做?

1 个答案:

答案 0 :(得分:1)

使用RelayCommand。这将帮助您在VM中编写命令逻辑。你会在这里得到它.. http://mvvmfoundation.codeplex.com/