MVVMCross WPF网格行命令不传递参数

时间:2017-02-15 15:00:44

标签: c# wpf mvvm mvvmcross

WPF 4.6我有一个列表网格,并希望根据行的选择打开一个视图,并传递ID。我没有得到任何细节新内容:

Grid ListView XAML

<DataGridTemplateColumn Width="Auto">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate >
            <Button Content="Edit Line"
                 Command="{Binding DataContext.EditClientLineCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                 CommandParameter="{Binding}"
                 Margin="5" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

ListViewModel(CustomerInfo是行项目)

public DetailsParameters _thisparams;   //simple class with int index only

我选择了一行时设置了这个:

public CustomerInfo SelectedCustomer
    {
        get { return _selectedCustomer; }
        set
        {
            _selectedCustomer = value;
            _thisparams.Index = _selectedCustomer.Id;
            RaisePropertyChanged(() => SelectedCustomer);
        }
    }

然后作为参数传递

private MvxCommand<CustomerInfo> _goToDetails;
public MvxCommand<CustomerInfo> EditClientLineCommand
{
    get {  return _goToDetails ?? 
        (_goToDetails = new MvxCommand<CustomerInfo>
            (SelectedCustomer => {
                ShowViewModel<CustomerDetailViewModel>
            (_thisparams);}
        )
        );
    }
}

和DetailViewModel启动区:

public void Init( DetailsParameters thisid)
{
    _customerId = thisid.Index;
}

customerId0

此外,然后我退出并重新选择另一行,我得到详细信息(仍然没有分配),但没有点击Init断点..

2 个答案:

答案 0 :(得分:0)

您应该在绑定中设置Mode=FindAncestor

 RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}

答案 1 :(得分:0)

问题1修复: 我将DetailsParameters课程更改为static - 这对我来说很有意义。在Stuart的文档中,当他谈到这个课程时,它是一个标准的实例课......所以我仍然担心我没有遵循正确的设计模式。

较小概率2理解:MVVMCross具有视图堆栈的概念,其中视图被创建并在导航上重新使用(转发)。根据设计,它将返回先前创建的视图并将其向前移动,而没有任何机会检查数据......我已经阅读了大量的信息,这是设计的&#39;评论,但对于我的生活,我不明白为什么你会默认这样做,而没有一个简单的模式来刷新数据... 我还未找到解决方法,并会发布一个单独的问题。 (在使用Caliburn,MVVMLight并发现问题之后......总有一些东西!!)(Stuart在Mvx上做得非常出色,但是我在Mvx上的表现只有2天,所以也许我&# 39; m缺少某些东西)..