关于在DataGrid如何提取每行的绑定的Tag值(取Tag的目地是为了进行某些操作)

    xiaoxiao2022-07-07  157

    这个问题都快想疯了。。。。最后出去逛了一圈,回来之后瞎写居然写出来了。

    ObservableCollection<DataArraly> list = new ObservableCollection<DataArraly>(); //首先简单介绍一下我绑定List,关于这个我本人前面的博客有具体讲 list.Add(new DataArraly() { ID = "1", Remark = " * ", Type = "Absorbance", Xunits = "nm", FID = "1", Color = "Cyan(ABS1)" });// list.Add(new DataArraly() { ID = "2", Remark = " * ", Type = "Absorbance", Xunits = "nm", FID = "2", Color = "Orange(ABS2)" });// list.Add(new DataArraly() { ID = "3", Remark = " * ", Type = "Absorbance", Xunits = "nm", FID = "3", Color = "Green(ABS3)" });// list.Add(new DataArraly() { ID = "4", Remark = " * ", Type = "Absorbance", Xunits = "nm", FID = "4", Color = "Blue(ABS4)" });// list.Add(new DataArraly() { ID = "5", Remark = " * ", Type = "Absorbance", Xunits = "nm", FID = "5", Color = "Purple(ABS5)" });// list.Add(new DataArraly() { ID = "6", Remark = " * ", Type = "Absorbance", Xunits = "nm", FID = "6", Color = "Brown(ABS6) " });// list.Add(new DataArraly() { ID = "7", Remark = " * ", Type = "Absorbance", Xunits = "nm", FID = "7", Color = "Black(ABS7)" });// list.Add(new DataArraly() { ID = "8", Remark = " * ", Type = "Absorbance", Xunits = "nm", FID = "8", Color = "Gray(ABS8)" });// list.Add(new DataArraly() { ID = "9", Remark = " * ", Type = "Absorbance", Xunits = "nm", FID = "9", Color = "OrangeRed(ABS9)" });// list.Add(new DataArraly() { ID = "10", Remark = " * ", Type = "Absorbance", Xunits = "nm", FID = "10", Color = "MediumSpringGreen(ABS10)" });//

    前端代码一部分

    <DataGridTemplateColumn Header="View" Width="0.7*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox Click="CheckBox_Click" Tag="{Binding FID, Mode=TwoWay}" Name="checkbox_1" IsChecked="True"></CheckBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>

    我的Tag是绑定了FID,为了实现某种功能,我必须在选中表格的某行时提取到这个“FID”值。 例如下表,我要提取4这行我绑定的标签值FID 代码具体如下,大概的想法:我选中这行时得到List的下标,然后根据下标得到数组List中关于FID的值。 ***list[_rowIndex].FID==(object) “4”***这句是核心

    private void Remove_Line_Click(object sender, RoutedEventArgs e) { int _rowIndex = 0; int _columnIndex = 0; if (GetCellXY(DataGrid_Measure2, ref _rowIndex, ref _columnIndex)) { if ( list[_rowIndex].FID==(object) "4")//这一句就找到了我绑定的Tag值FID { this.ShowDemarcatezedGraph.GraphPane.CurveList.Remove(ABS1); ShowDemarcatezedGraph.AxisChange(); ShowDemarcatezedGraph.Refresh(); } } } private bool GetCellXY(DataGrid dg, ref int rowIndex, ref int columnIndex) { var _cells = dg.SelectedCells;//获取选中单元格的列表 if (_cells.Any()) { rowIndex = dg.Items.IndexOf(_cells.First().Item); columnIndex = _cells.First().Column.DisplayIndex; return true; } return false; }
    最新回复(0)