datagrid的选择变为事件并未以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

protected override void InitializeCommands()
        {
            DgSelectionChangedCommand= new DelegateCommand<object>(DGSelectionChanged);
        }

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

    void DGSelectionChanged(object obj)
    {
        //Logic
    }

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

RelativeSourceAncestorLevel设置为 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>

autoPostback是一种机制,该页面将根据Web控件中的某些事件自动将页面发布回服务器。在某些Web控件中,该属性称为自动发布后。

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

autopostback = true;

请浏览链接以获取更多信息:AutoPostback = true和autopostback = false?

之间的区别

相关内容

  • 没有找到相关文章

最新更新