如何获取所选单元格的wpf数据网格中的行标题值?

时间:2013-12-27 11:13:45

标签: c# wpf xaml datagrid

我得到了细胞价值&此代码中的单元标头值。如何获取所选单元格的wpf数据网格中的行标题值?

XAML -

<DataGrid Name="Grid1" Height="550" Width="850" AutoGenerateColumns="True" 
          CanUserResizeRows="False" CanUserDeleteRows="False"
          CanUserAddRows="False" AreRowDetailsFrozen="False"
          SelectionUnit="Cell" SelectedCellsChanged="Grid1_SelectedCellsChanged" 
          CellEditEnding="Grid1_CellEditEnding" LoadingRow="Grid1_LoadingRow">
   <DataGrid.Style>
      <Style TargetType="DataGrid">
         <Setter Property="AlternatingRowBackground" Value="LightYellow"/>
      </Style>
   </DataGrid.Style>
</DataGrid>

代码 -

我如何操纵列标题是 -

 private DataTable _GameData;

public Grid()
        {
            _GameData = new DataTable();
            _GameData.Columns.Add(new DataColumn("Sunday", typeof(string)));
            _GameData.Columns.Add(new DataColumn("Monday", typeof(string)));
            _GameData.Columns.Add(new DataColumn("Tuesday", typeof(string)));
            _GameData.Columns.Add(new DataColumn("Wednesday", typeof(string)));
            _GameData.Columns.Add(new DataColumn("Thursday", typeof(string)));
            _GameData.Columns.Add(new DataColumn("Friday", typeof(string)));
            _GameData.Columns.Add(new DataColumn("Saturday", typeof(string)));

            for(int i=0;i<=23;i++)
            {
                var row = _GameData.NewRow();
                _GameData.Rows.Add(row);
                row["Sunday"] = "";
                row["Monday"] = "";
                row["Tuesday"] = "";
                row["Wednesday"] = "";
                row["Thursday"] = "";
                row["Friday"] = "";
                row["Saturday"] = "";            
            }
 InitializeComponent();

            Grid1.ItemsSource = _GameData.AsDataView();
            Grid1.RowHeaderWidth = 50;
            Grid1.ColumnWidth = 100;
        }

 public DataTable GameData
        { get { return _GameData; } }

我如何操纵行标题 -

 private void Grid1_LoadingRow(object sender, DataGridRowEventArgs e)
        {                    
            var id = e.Row.GetIndex();  


            switch (id)
            {
                case 0:
                    {
                        e.Row.Header = "12-00";
                        break;
                    }
                case 1:
                    {
                        e.Row.Header = "12-30";
                        break;
                    }
                case 2:
                    {
                        e.Row.Header = "1-00";
                        break;
                    }
                case 3:
                    {
                        e.Row.Header = "1-30";
                        break;
                    }
                case 4:
                    {
                        e.Row.Header = "2-00";
                        break;
                    }
                case 5:
                    {
                        e.Row.Header = "2-30";
                        break;
                    }
                case 6:
                    {
                        e.Row.Header = "3-00";
                        break;
                    }
                case 7:
                    {
                        e.Row.Header = "3-30";
                        break;
                    }
                case 8:
                    {
                        e.Row.Header = "4-00";
                        break;
                    }
                case 9:
                    {
                        e.Row.Header = "4-30";
                        break;
                    }
                case 10:
                    {
                        e.Row.Header = "5-00";
                        break;
                    }
                case 11:
                    {
                        e.Row.Header = "5-30";
                        break;
                    }
                case 12:
                    {
                        e.Row.Header = "6-00";
                        break;
                    }
                case 13:
                    {
                        e.Row.Header = "6-30";
                        break;
                    }
                case 14:
                    {
                        e.Row.Header = "7-00";
                        break;
                    }
                case 15:
                    {
                        e.Row.Header = "7-30";
                        break;
                    }
                case 16:
                    {
                        e.Row.Header = "8-00";
                        break;
                    }
                case 17:
                    {
                        e.Row.Header = "8-30";
                        break;
                    }
                case 18:
                    {
                        e.Row.Header = "9-00";
                        break;
                    }
                case 19:
                    {
                        e.Row.Header = "9-30";
                        break;
                    }
                case 20:
                    {
                        e.Row.Header = "10-00";
                        break;
                    }
                case 21:
                    {
                        e.Row.Header = "10-30";
                        break;
                    }
                case 22:
                    {
                        e.Row.Header = "11-00";
                        break;
                    }
                case 23:
                    {
                        e.Row.Header = "11-30";
                        break;
                    }               
                default:
                    {
                        break;
                    }
            }

获得了列标题&amp;单元格值 -

 private void Grid1_SelectedCellsChanged(object sender,
                                         SelectedCellsChangedEventArgs e)
 {
     foreach (var item in e.AddedCells)
     {
        var col = item.Column as DataGridColumn;

        MessageBox.Show(""+col.Header);

        var fc = col.GetCellContent(item.Item);
        if (fc is TextBlock)
        {
            MessageBox.Show("Values" + (fc as TextBlock).Text);
        }                
     }
 }

输出 - 消息显示所选列标题值,箭头说明要显示的行标题。

enter image description here

1 个答案:

答案 0 :(得分:5)

您需要先DataGridRow访问其Header

DataGridRow你可以像这样使用发件人dataGrid的ItemContainerGenerator.ContainerFromItem()方法:

private void Grid1_SelectedCellsChanged(object sender,
                                        SelectedCellsChangedEventArgs e)
{
    DataGrid dg = (DataGrid)sender;
    foreach (var item in e.AddedCells)
    {
        DataGridRow row = 
           (DataGridRow)dg.ItemContainerGenerator.ContainerFromItem(item.Item);

        var col = item.Column as DataGridColumn;

        MessageBox.Show("" + col.Header);

        var fc = col.GetCellContent(item.Item);
        if (fc is TextBlock)
        {
            MessageBox.Show("Values" + (fc as TextBlock).Text);
        }

        MessageBox.Show("Row Header " + row.Header.ToString());
    }
}