简体   繁体   中英

MVVMCross WPF Grid Row Command not passing parameter

WPF 4.6 I've a list grid, and would like to open a view based on the selection of a row, passing the ID. I'm getting nothing passed to the details new:

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 is the row Item)

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

I set this when a row is selected:

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

and then pass as a parameter

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

and the DetailViewModel startup area:

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

customerId is 0

Also, then I back out and reselect another row, I get to the detail (still nothing assigned), but without hitting the Init breakpoint..

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

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

Prob 1 fixed: I changed the DetailsParameters class to static - which makes sense to me. In Stuart's documentation when he talked about this class, it's a standard instance class... so I'm still worried I'm not following the correct design pattern.

Lesser prob 2 understood: MVVMCross has the concept of the view stack where the view is created and re-used (brought forwards) on navigation. By design it will return the previously created view and bring it forwards, without any opportunity to 'check the data'... I've read numerous 'this is by design' comments, but for the life of me I can't see why you would do this by default, and not have a simple pattern to refresh the data... I've yet to find a way around that, and will post a separate question. (after using Caliburn, MVVMLight and finding issues with those... there is always something!!) (Stuart's done a brilliant job with Mvx, but I've only 2 days exp with Mvx so perhaps I'm missing something)..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM