【问题标题】:SelectionChanged event of datagrid is not firing in mvvm way数据网格的 SelectionChanged 事件未以 mvvm 方式触发
【发布时间】:2017-12-19 07:02:56
【问题描述】:

//我有一个嵌套数据网格 当内部网格行选择发生变化时,我需要触发事件。

<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中我的Delegate Command初始化

protected override void InitializeCommands()
        {

            DgSelectionChangedCommand= new DelegateCommand<object>(DGSelectionChanged);

        }

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

    void DGSelectionChanged(object obj)
    {
        //Logic
    }

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

【问题讨论】:

  • 唯一能想到的就是命令绑定;由于该命令存在于您的 ViewModel 中,因此将 RelativeSource 设置为 Window / UserControl 可能会起作用。
  • 我在一周或更早之前在 DataGrid 中看到过具有相同属性的这个问题
  • stackoverflow.com/q/47790738/5605739 我发现这是你自己的问题
  • 您可以通过删除交互触发器并将UpdateSourceTrigger = PropertyChanged 添加到您的`SelectedItem` 来解决此问题
  • UpdateSourceTrigger=PropertyChanged 是 SelectedItem 属性上绑定的默认值。无需设置。

标签: c# wpf mvvm datagrid


【解决方案1】:

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>

【讨论】:

    【解决方案2】:

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

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

    AutoPostBack=True;

    请通过链接了解更多信息: Difference between AutoPostBack=True and AutoPostBack=False?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 2018-10-24
      • 2014-05-02
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      相关资源
      最近更新 更多