2009/12/12作成
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
[WPF]DataGridの指定セルにフォーカスする
writer:オレ 2013-01-09(Wed) .NET
[WPF]ViewModelを介してDataGridにバインディングするにボタンを一つ配置し、以下のようなクリックハンドラとメソッドを作る。
private void button1_Click(object sender, RoutedEventArgs e)
{
int rowIndex = 0;
int colIndex = 1;
// 行、列の番号を指定してDataGridCellを取得する
DataGridCell cell = GetDataGridCell(dataGrid1, rowIndex, colIndex);
// データグリッドセルにフォーカスする
FocusManager.SetFocusedElement(this, cell);
}
public DataGridCell GetDataGridCell(DataGrid dataGrid, int rowIndex, int columnIndex)
{
if (dataGrid.Items == null || dataGrid.Items.IsEmpty)
{
return null;
}
DataGridRow row = GetDataGridRow(dataGrid, rowIndex);
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
if (cell == null)
{
// Visual Treeが構築されていない場合もあるため更新
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(row, dataGrid.Columns[columnIndex]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
}
return cell;
}
return null;
}
public DataGridRow GetDataGridRow(DataGrid dataGrid, int index)
{
if (dataGrid.Items == null || dataGrid.Items.IsEmpty)
{
return null;
}
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// Visual Treeが構築されていない場合もあるため更新
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(dataGrid.Items[index]);
row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T result = default(T);
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
Visual child = (Visual)VisualTreeHelper.GetChild(parent, i);
result = child as T;
if (result == null)
{
result = GetVisualChild<T>(child);
}
if (result != null)
{
break;
}
}
return result;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
int rowIndex = 0;
int colIndex = 1;
// 行、列の番号を指定してDataGridCellを取得する
DataGridCell cell = GetDataGridCell(dataGrid1, rowIndex, colIndex);
// データグリッドセルにフォーカスする
FocusManager.SetFocusedElement(this, cell);
}
public DataGridCell GetDataGridCell(DataGrid dataGrid, int rowIndex, int columnIndex)
{
if (dataGrid.Items == null || dataGrid.Items.IsEmpty)
{
return null;
}
DataGridRow row = GetDataGridRow(dataGrid, rowIndex);
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
if (cell == null)
{
// Visual Treeが構築されていない場合もあるため更新
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(row, dataGrid.Columns[columnIndex]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
}
return cell;
}
return null;
}
public DataGridRow GetDataGridRow(DataGrid dataGrid, int index)
{
if (dataGrid.Items == null || dataGrid.Items.IsEmpty)
{
return null;
}
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// Visual Treeが構築されていない場合もあるため更新
dataGrid.UpdateLayout();
dataGrid.ScrollIntoView(dataGrid.Items[index]);
row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T result = default(T);
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
Visual child = (Visual)VisualTreeHelper.GetChild(parent, i);
result = child as T;
if (result == null)
{
result = GetVisualChild<T>(child);
}
if (result != null)
{
break;
}
}
return result;
}
PR
この記事にコメントする
この記事へのコメント
新着記事
2013 - 06 - 27
2013 - 06 - 27
2013 - 06 - 19
2013 - 01 - 11
2013 - 01 - 11
カテゴリー
アーカイブ
検索
新着コメント
ブックマーク