0

//I am having a Nested datagrid and i need to fire the event when inner grid row selection changes.

<DataGrid ScrollViewer.CanContentScroll="True"
                              ScrollViewer.HorizontalScrollBarVisibility="Auto"                          
                              HeadersVisibility="Row"
                              Height="{Binding ElementName=itemsgrid,Path=ActualHeight}"
                              Grid.Row="1"
                              RowDetailsVisibilityMode="Visible"
                              CanUserAddRows="false"
                              VerticalAlignment="Top"
                              AutoGenerateColumns="False"
                              SelectedItem="{Binding SelectedBasketItem}"
                              ItemsSource="{Binding SelectedOrderBasketItems}"
                              CanUserDeleteRows="True">
                        <DataGrid.RowDetailsTemplate>
                            <DataTemplate>
                                <DataGrid ScrollViewer.CanContentScroll="True"
                                          ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                          HorizontalAlignment="Stretch"
                                          HorizontalContentAlignment="Stretch"
                                          CanUserAddRows="false"
                                          HeadersVisibility="Row"
                                          AutoGenerateColumns="False"
                                          ItemsSource="{Binding SelectedBasketItems}"
                                          SelectedValue="{Binding SelectedBasketItemValue}"
                                          SelectedIndex="{Binding SelectedOrderItemIndex}"
                                          SelectedItem="{Binding SelectedSpecificItemInBasket, Mode=TwoWay}"

                                          DataGrid.SelectionMode="Single"
                                          Style="{StaticResource GridItemsControlStyle}"
                                          ctrls:DragDrop.DropHandler="{Binding DropHandler}">

                           <i:Interaction.Triggers>
                                <i:EventTrigger EventName="SelectionChanged">
                                    <i:InvokeCommandAction Command="{Binding DataContext.DgSelectionChangedCommand, 
                                                RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                                           CommandParameter="{Binding}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>

                                    <DataGrid.Columns>
                                        <DataGridTextColumn IsReadOnly="True"
                                                            Binding="{Binding Path=ItemName}"
                                                            Width="195">                                        
                                        </DataGridTextColumn>

                                        <DataGridTemplateColumn>
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Label Content="{Binding Path=RulesCount}"
                                                           Background="{Binding Path=RulesCount ,Converter={StaticResource ItemColourConverter}}" />
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>

                                        <DataGridTemplateColumn Width="115">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Label Content="{Binding Path=LinkedOrderCount}"
                                                           Background="{Binding Path=LinkedOrderCount ,Converter={StaticResource ItemColourConverter}}" />

                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>

                                        <DataGridTemplateColumn Width="55">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Label Content="{Binding Path=SDICount}">
                                                        <Label.Background>
                                                            <MultiBinding Converter="{StaticResource SdiItemColourConverter}">
                                                                <Binding ElementName="SDIMaxCnt"
                                                                         Path="Value" />
                                                                <Binding Path="SDICount" />
                                                            </MultiBinding>
                                                        </Label.Background>
                                                    </Label>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                    </DataGrid.Columns>
                                </DataGrid>
                            </DataTemplate>
                        </DataGrid.RowDetailsTemplate>

//My Delegate Command initialization in ViewModel

protected override void InitializeCommands()
        {

            DgSelectionChangedCommand= new DelegateCommand<object>(DGSelectionChanged);

        }

//My Method in the view model to be called when selection changed event is fired

    void DGSelectionChanged(object obj)
    {
        //Logic
    }

The event gets fired when i use the same event in code behind . i am trying to use interaction trigger to achieve the same in mvvm way .Not sure what i am missing .Any help is greatly appreciated.

CodeDada
  • 425
  • 1
  • 11
  • 24
  • The only thing I can think of is the command binding; since the command exists in your ViewModel maybe setting the `RelativeSource` to the `Window` / `UserControl` would work. – Jack Dec 19 '17 at 08:41
  • I've seen this question with the same properties in the DataGrid a week or more ago – Celso Lívero Dec 19 '17 at 10:59
  • https://stackoverflow.com/q/47790738/5605739 I found it was a question of your own – Celso Lívero Dec 19 '17 at 11:01
  • you may be able to resolve this by removing the interaction trigger and adding `UpdateSourceTrigger = PropertyChanged` to your` SelectedItem` – Celso Lívero Dec 19 '17 at 11:04
  • `UpdateSourceTrigger=PropertyChanged` is the default for a Binding on the SelectedItem property. No need to set it. – Clemens Dec 19 '17 at 15:15
  • @Celso Livero , what i am trying to achieve in that question and this is different. – CodeDada Dec 20 '17 at 06:45

2 Answers2

4

Set the AncestorLevel of the RelativeSource to 2:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding DataContext.DgSelectionChangedCommand, 
                    RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, AncestorLevel=2}}" 
                                   CommandParameter="{Binding}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
mm8
  • 163,881
  • 10
  • 57
  • 88
-3

Autopostback is the mechanism, by which the page will be posted back to the server automatically based on some events in the web controls. In some of the web controls, the property called auto post back.

which if set to true, will send the request to the server when an event happens in the control.

AutoPostBack=True;

please go through the link for more: Difference between AutoPostBack=True and AutoPostBack=False?