datagrid的SelectionChanged事件未以mvvm方式触发

时间:2017-12-19 07:02:56

标签: c# wpf mvvm datagrid

//我有一个嵌套的数据网格 我需要在内部网格行选择更改时触发事件。

<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>

// ViewModel中的My Delegate Command初始化

protected override void InitializeCommands()
        {

            DgSelectionChangedCommand= new DelegateCommand<object>(DGSelectionChanged);

        }

//触发选择更改事件时要调用的视图模型中的我的方法

    void DGSelectionChanged(object obj)
    {
        //Logic
    }

当我在后面的代码中使用相同的事件时,事件被触发。我正在尝试使用交互触发器以mvvm方式实现相同。不确定我缺少什么。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

AncestorLevel的{​​{1}}设置为RelativeSource

2

答案 1 :(得分:-3)

Autopostback是一种机制,通过该机制,页面将根据Web控件中的某些事件自动发回服务器。在某些Web控件中,该属性称为auto post back。

如果设置为true,则会在控件中发生事件时将请求发送到服务器。

的AutoPostBack = TRUE;

请仔细阅读以下链接: Difference between AutoPostBack=True and AutoPostBack=False?