複数の行を持つデータグリッドがあります。各行の最初の列はボタンです。行インデックスがあります。行インデックスが 7 だとしましょう。今、行 7 内のボタンを取得し、その内容を変更したいと考えています。
データグリッドの特定の行内でこのボタン コントロールを取得し、その値を変更するにはどうすればよいですか?
複数の行を持つデータグリッドがあります。各行の最初の列はボタンです。行インデックスがあります。行インデックスが 7 だとしましょう。今、行 7 内のボタンを取得し、その内容を変更したいと考えています。
データグリッドの特定の行内でこのボタン コントロールを取得し、その値を変更するにはどうすればよいですか?
おそらく、以下のコードで問題が解決します。
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="ShowHideDetails">Details</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Codebehind C# では、コントロールにアクセスします
private void ShowHideDetails(object sender, RoutedEventArgs e)
{
// You can access the button and do whateve the changes you want
Button objMyButton = null;
if (sender is Button)
{
objMyButton = (sender as Button);
}
//You can access the parent object which means corresponding DataGridRow and do whatever you want
for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
if (vis is DataGridRow)
{
var row = (DataGridRow)vis;
break;
}
}