【问题标题】:Passing SelectedCells Information to another DataGrid on different page将 SelectedCells 信息传递给不同页面上的另一个 DataGrid
【发布时间】:2014-02-19 10:56:51
【问题描述】:

我已经尝试了很多方法来解决这个问题,但我似乎找不到正确的答案。

我有一个有 6 列的 DataGrid,只有当用户单击 第 6 列 上的任何行时,他们才会被重定向到另一个页面。

因此用户只能单击To-Do 下的任何单元格。当他们单击该行时,我需要它来找到与该单元格位于同一行的Type例如如果我点击了 6 To-Do,它应该知道我点击了行类型Football

然后我希望将第一列中的Type 传递到另一个页面,这样我就可以在Type 下调出所有不同的To-Do 项目。

这是我尝试过的:

        cSViewEntity selectedView = dgFake.SelectedItem as cSViewEntity;

        AllocateAudits page = new AllocateAudits()
        {
            DataContext = selectedView
        };
        FrameNavigation.Navigate(page);

但是当我这样做时,它并没有达到这个方法:

    public void Page_OnNavigatedTo(object sender, NavigationEventArgs e)
    {

        var newDataContext = e.Content;

        _View = (cSViewEntity)newDataContext;

        _Transactions = new List<cAuditTransactionTypeEntity>();

        _Transactions = sp.GetTransactionTypes().ToList();

         string TransactionType = _View.TransactionType;

        int TransactionTypeID = _Transactions.FirstOrDefault(w => w.TransactionType == TransactionType).TransactionTypeId;

        dgAllocate.ItemsSource = sp.GetTransactionsNotEvaluated(TransactionTypeID).ToList();
    }

任何帮助都会非常有帮助,

谢谢。

问题答案:

我将 SelectedItem 传递到另一个页面,然后在该页面上找到该类型并在该类型上运行存储过程。

cSViewEntity obj = (cSViewEntity)dgFake.SelectedItem;
     AllocateAudits page = new AllocateAudits(obj)
     {
         DataContext = obj
     };
         FrameNavigation.Navigate(page);
     }

然后将它传递给构造函数,然后在方法的开头添加它。

 private void Rawr(cSViewEntity obj)
 {
      var transactiontype = _obj.TransactionType;

      //Stores the information from newDataContext to the entity with the name of _View.
      _View = (cSViewEntity)obj;

【问题讨论】:

    标签: c# wpf datagrid cells datagridcell


    【解决方案1】:

    对于最后一列(第 6 列),我建议您创建一个 Datagrid 模板单元。

    例如:

    <DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
     <DataGrid.Columns>
      <DataGridTemplateColumn Header="To Do" >
       <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
         <Button x:Name="MyNavigationButton" Content="{Binding ToDo}" Click="MyClickEvent"/>
        </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
     </DataGrid.Columns>
    </DataGrid>
    

    在后台代码中,运行 MyClickEvent

    private void MyClickEvent(object sender, EventArgs e)
    {
     var myBTNData = ((Button)sender).DataContext;
    
     //here does follow your code based on the myBTNData
    }
    

    希望它能帮助您入门。您还可以使用其他元素(如超链接)

    【讨论】:

    • 感谢您的回答,问题是我不想添加按钮或复选框,我只是希望用户单击该行。这是否可以通过单击单元格甚至仅单击行来找到类型?
    • 您也可以尝试使用 mousedown 事件,但这始终是行事件。
    • 要用我得出的答案更新我的问题。
    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2016-04-24
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 2019-05-31
    • 2021-03-11
    相关资源
    最近更新 更多