WPF DataGrid:如何访问DataGridTemplateColumn的特定行中的ComboBox? [英] WPF DataGrid: How do I access a ComboBox in a specific row of a DataGridTemplateColumn?
问题描述
我的应用使用以下XAML代码正确地使用ComboBoxes填充了DataGrid列:
My app correctly populates a DataGrid column with ComboBoxes, using the following XAML code:
<DataGridTemplateColumn Header="Thickness">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding SteelThickness, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding BottomPlateThickness, UpdateSourceTrigger=PropertyChanged}" SelectionChanged="ComboBox_SelectionChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我正在尝试找出一个表达式,例如
I am trying to figure out an expression such as
ComboBox cmb = dataGrid [i] .Column [11] .ComboBox
一次将检索一个ComboBox.
which will retrieve one ComboBox at a time.
TIA
推荐答案
您好,在您回答了我的问题之后,我决定编写一个帮助程序类以通过索引访问行和列.我正在尝试提出一个想法.我没有对其进行良好的测试,因此可能存在一些问题.
Hi after going through your question I decided to write a helper class for accesing Rows and columns by indices.I am trying to give an idea . I have not tested it well so there might be some issues.
完整的解决方案通过索引访问datagrid行和列
//此类将有助于按索引获取行,列或单元格.虽然可能没有对null和索引的适当检查,但抱歉.稍后我将对其进行更新.
//This class will help to get Row, Column or Cell by indices. Though there might not be some proper check for null and indices Sorry for that.I will update it later.
public static class DataGridExtension
{
public static DataGridColumn GetColumnByIndices(this DataGrid dataGrid, int rowIndex, int columnIndex)
{
ValidateParameters(dataGrid, rowIndex, columnIndex);
var row = dataGrid.GetRowByIndex(rowIndex);
if (row != null)
return row.GetRowColumnByIndex(columnIndex);
return null;
}
public static DataGridCell GetCellByIndices(this DataGrid dataGrid, int rowIndex, int columnIndex)
{
ValidateParameters(dataGrid, rowIndex, columnIndex);
var row = dataGrid.GetRowByIndex(rowIndex);
if (row != null)
return row.GetRowCellByColumnIndex(columnIndex);
return null;
}
//TODO:Validate RowIndex
public static DataGridRow GetRowByIndex(this DataGrid dataGrid, int rowIndex)
{
if (dataGrid == null)
return null;
return (DataGridRow)dataGrid.ItemContainerGenerator
.ContainerFromIndex(rowIndex);
}
//TODO:Validate ColumnIndex
public static DataGridColumn GetRowColumnByIndex(this DataGridRow row, int columnIndex)
{
if (row != null)
{
var cell=GetRowCellByColumnIndex(row, columnIndex);
if(cell!=null)
return cell.Column;
}
return null;
}
//TODO:Validate ColumnIndex
public static DataGridCell GetRowCellByColumnIndex(this DataGridRow row, int columnIndex)
{
if (row != null)
{
DataGridCellsPresenter cellPresenter = row.GetVisualChild<DataGridCellsPresenter>();
if (cellPresenter != null)
return ((DataGridCell)cellPresenter.ItemContainerGenerator.ContainerFromIndex(columnIndex));
}
return null;
}
private static void ValidateParameters(DataGrid dataGrid,int rowIndex,int columnIndex)
{
if (dataGrid == null)
throw new ArgumentNullException("datagrid is null");
if (rowIndex >= dataGrid.Items.Count)
throw new IndexOutOfRangeException("rowIndex out of Index");
if (columnIndex >= dataGrid.Columns.Count)
throw new IndexOutOfRangeException("columnIndex out of Index");
}
}
****//该类将有助于找到VisualChild ****
****//This Class will help to find the VisualChild ****
public static class VisualHelper
{
public static T GetVisualChild<T>(this Visual parent) where T : Visual
{
T child = default(T);
for (int index = 0; index < VisualTreeHelper.GetChildrenCount(parent); index++)
{
Visual visualChild = (Visual)VisualTreeHelper.GetChild(parent, index);
child = visualChild as T;
if (child == null)
child = GetVisualChild<T>(visualChild);//Find Recursively
if (child != null)
break;
}
return child;
}
}
现在,您可以使用这些类来按索引获取Columns,Cells,Rows之类的
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//Now we can get Column like this.
var column = dataGrid.GetColumnByIndices(1, 1);
//As SO want to find the ComboBox within that Column
ComboBox comboBox;
var cell = dataGrid.GetCellByIndices(1, 1); //DataGridColumn Does'nt Inherit Visual class so using GetCellByIndices
if(cell!=null)
comboBox = cell.GetVisualChild<ComboBox>(); //DataGridCell Inherit Visual so we can use our VisualHelper Method
}
这篇关于WPF DataGrid:如何访问DataGridTemplateColumn的特定行中的ComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!