WPF绑定到Datagrid内部的Datacontext的命令

我刚刚开始使用WPF和MVVM,并设法将 Datagrid 绑定到DataContext ViewModel类的列表 ProductList 。但是,我现在尝试将DataGrid内部的按钮绑定到DataContext类的某些命令。

<DataGrid x:Name="myDataGrid" ItemsSource="{Binding ProductsList}" x:Fieldmodifier="public" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="No MAT" Binding="{Binding MATProductNumber}" Width="0.1*"/>
            <DataGridTextColumn Header="Format" Binding="{Binding tblFormat.FormatName}" Width="0.1*"/>
            <DataGridTextColumn Header="Nom produit" Binding="{Binding tblProduct.ProductName}" Width="0.1*"/>
            <DataGridTextColumn x:Name="productGradeHeader" Header="Grade" Binding="{Binding tblGrade.GradeName}" Width="0.1*"/>
            <DataGridTemplateColumn Width=".1*"           
                <DataGridTemplateColumn.HeaderTemplate> 
                     <DataTemplate>
                       <StackPanel Orientation="Horizontal">
                          <Button x:Name="leftArrowSearchButton" Command="{Binding PreviousPageCommand}" commandparameter="1">
                            <materialDesign:PackIcon Kind="ArrowLeftThick" Width="25" Height="25" VerticalAlignment="Center"/>
                          </Button>
                          <Button x:Name="rightArrowSearchButton" Command="{Binding NextPageCommand}" commandparameter="1">
                            <materialDesign:PackIcon Kind="ArrowRightThick" Width="25" Height="25" VerticalAlignment="Center"/>
                          </Button>
                       </StackPanel>
                     </DataTemplate>
               </DataGridTemplateColumn.HeaderTemplate>
           </DataGridTemplateColumn>
   </DataGrid.Columns>
</DataGrid>

此行应该在我的数据上下文中绑定到 PreviousPageCommand

<Button x:Name="leftArrowSearchButton" Command="{Binding PreviousPageCommand}" commandparameter="1"  ToolTip="Voir la page précédente" Height="24" Width="53" Margin="0,20,0">

我已经调试,发现它甚至没有调用该函数。因此,我几乎可以确定绑定存在问题。

似乎我的按钮正在尝试绑定到ProductsList的元素。有没有办法回到上一级?我已经尝试将按钮绑定到DataContext.PreviousPageCommand

预先感谢!

编辑1:这是后端中要求的代码

 public ObservableCollection<tblMATProduct> ProductsList { get; set; }

 private void NextPageScrollCommand(string number)
 {
     Page++;
     navigate(Page,Size);
 }

 public void navigate(int page,int size)
 {
     var result = new ObservableCollection<tblMATProduct>(db.tblMATProduct.Include(s => s.tblGrade).Include(s => s.tblProduct).Include(s => s.tblFormat).OrderBy(s => s.MATProductID).Skip(page * size).Take(size).ToList());
     ProductsList = result;
 }

ProductsList正在更新,我可以在调试器中看到它。只是UI没有更新。

编辑2:现在工作正常!刚刚将此行添加到我的ProductsList设置器中:

set
        {
            SetProperty(ref _products,value);
        }
tracyjuice 回答:WPF绑定到Datagrid内部的Datacontext的命令

如我的评论所述,您需要绑定到(ViewModel)Parent/Ancestor.DataContext

Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},Path=DataContext.PreviousPageCommand}"

您还需要绑定SelectedItem中的DataGrid,以便ViewModel知道用户单击时按钮位于哪一行。或将按钮的DataContext绑定到CommandParameter。就像评论中提到的 @Andy

,

您好,您可以按照以下步骤操作:

{Binding DataContext.PreviousPageCommand,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}

但是仅当DataGrid.DataContext更改时,绑定才会更新,而DataContext.PreviousPageCommand更改时,绑定不会更新。

本文链接:https://www.f2er.com/2413256.html

大家都在问