简体   繁体   中英

Wpf data grid CurrentCell property fires only once MVVM

I have a datagrid that looks like:

<DataGrid x:Name="Applications" CanUserResizeColumns="False" CanUserResizeRows="False"  
 AutoGenerateColumns="false" CanUserAddRows="false" ItemsSource="{Binding Applications}" 
 SelectionMode="Single"
 CurrentCell="{Binding CellInfo, Mode=TwoWay}">

And I have a question about CurrentCell, it is binded to poeprty in view model that looks like:

    private DataGridCellInfo cellInfo;
    public DataGridCellInfo CellInfo
    {
        get => cellInfo;
        set
        {
            cellInfo = value;
            OnPropertyChanged();

            if (cellInfo.Column.DisplayIndex == 1)
            {
                var selectedApplication = (ExtendedApplicationFile)cellInfo.Item;
                ExpandAppDetailsCommand.Execute(selectedApplication);
            }
        }
    }

And what it does, it sets correct item and sends it to command that will expend and hide row details window.

Problem is if I click once property is set and it will expand, but when I click second time on same cell, property is not setting and details row is not collapsing. It will work again when I click other cell and get back to it, but that is not I am aiming for.

Basing on information in comment I came up with simple solution, Ive added cell template with event trigger:

<DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
             <Label Content="{Binding Name}" Width="350">
                   <i:Interaction.Triggers>
                         <i:EventTrigger EventName="MouseDown">
                                  <i:InvokeCommandAction Command="{Binding DataContext.ExpandAppDetailsCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" CommandParameter="{Binding}"/>
                          </i:EventTrigger>
                    </i:Interaction.Triggers>
                 </Label>
           </DataTemplate>                
</DataGridTemplateColumn.CellTemplate>

Using thin each time I click cell event fires and toggles details view.

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