【问题标题】:How to Scroll to Newly added row in datagrid in wpf mvvm如何滚动到 wpf mvvm 中数据网格中新添加的行
【发布时间】:2015-02-15 14:56:05
【问题描述】:

我有一个绑定 Observable Collection 的数据网格。当我单击添加新按钮时,我会将新行添加到集合中。我如何务实地滚动到新行。

数据网格 XMAL

<DataGrid SelectedIndex="{Binding SelectedIntex}" IsEnabled="{Binding IsKeySet}" CanUserDeleteRows="False" CanUserAddRows="False" Name="dgwTemplateDetails" SelectionMode="Single" ItemsSource="{Binding OrderTemplateList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedOrderTemplate}" IsReadOnly="False" AutoGenerateColumns="False" Width="auto">

      <DataGrid.Columns>
                            <DataGridTextColumn Header="Srl No" Visibility="Hidden" Binding="{Binding SrlNo}"/>
                            <DataGridTextColumn Header="Act Code" Width="75" Binding="{Binding ActCode, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridTextColumn Header="Act Name" Width="275" Binding="{Binding ActName, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridTextColumn Header="No. Of Days" Width="75" Binding="{Binding NoOfDays, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridCheckBoxColumn Header="Is Cutting" Width="75" Binding="{Binding IsCutSelected, UpdateSourceTrigger=PropertyChanged}" />

        </DataGrid.Columns>
                    </DataGrid>

VM 添加行函数

if (ValidateHeader())
{
  if (OrderTemplateList == null)
    this.OrderTemplateList = new ObservableCollection<EventManagementTemplate>();
  EventManagementTemplate obJEvent = new EventManagementTemplate();
  obJEvent.BuyerCode = this.BuyerCode;
  this.OrderTemplateList.Add(obJEvent);
  int no = 1;
  this.OrderTemplateList.ToList().ForEach(m => m.SrlNo = no++);
}

【问题讨论】:

    标签: c# wpf mvvm datagrid observablecollection


    【解决方案1】:

    您需要执行以下操作:

    1. 为您的 dataGrid 命名,以便您可以在代码隐藏文件(与包含您的数据网格的 xaml 文件配对的 .cs 文件)中访问它

    2. 向您的视图模型添加一个委托,以供 AddRow 函数调用 - 该委托应将您希望滚动到视图中的对象作为参数。

    3. 让包含数据网格的代码订阅委托 ==> 基本上,后面的代码是为您的视图模型提供回调。回调后面的代码将是滚动到新项目的代码。

    4. 回调应调用数据网格的 ScrollIntoView 函数 (http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.scrollintoview(v=vs.110).aspx)

    5. 在视图模型的 Add Row 函数结束时(添加项目之后)调用委托,并将项目滚动到视图中。

    示例代码:

    public class MyViewModel
    {
       // declare the delegate signature...
       delegate void ScrollIntoViewDelegateSignature(EventManagementTemplate objEvent);
    
       // create a pointer to the delegate that can be set by the code behind...
       public ScrollIntoViewDelegateSignature ScrollIntoView {get; set;}
    
       protected AddRow()
       {
          .. your code here
    
          // call the delegate...
          if (ScrollIntoView != null)
             ScrollIntoView(objEvent)
       }
    }
    
    public class MyControlOrWindowThatContainsDataGrid : UserControl/ChildWindow/Page
    {
       public void Initialize()
       {
          ...your code here
          // set the scrollIntoView delegate...
          myViewModel.ScrollIntoView = ScrollIntoView;
       }
    
       // create a ScrollIntoView function
       // ==> the return value and parameters MUST match the delegate signature
       public void ScrollIntoView(EventManagementTemplate objEvent)
       {
          myDataGrid.ScrollIntoView(objEvent);
       }
    }
    

    参考 http://msdn.microsoft.com/en-us/library/900fyy8e.aspx

    在我看来,最好使用事件来执行此操作,但这更复杂。 这是有关如何使用事件的 msdn 文档:http://msdn.microsoft.com/en-us/library/awbftdfh.aspx

    【讨论】:

    • 我不知道如何将委托功能添加到视图模型。你能描述一下吗?
    • 我在回答中添加了一些示例代码来解释代表。
    【解决方案2】:

    在 DataGrid 中添加项目后试试这个:

    dgwTemplateDetails.ScrollIntoView(obJEvent);
    

    这一行将滚动到当前添加的元素。

    【讨论】:

      猜你喜欢
      • 2014-07-28
      • 2014-06-02
      • 2013-11-18
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多