将按钮添加到 WPF DataGrid
Posted
技术标签:
【中文标题】将按钮添加到 WPF DataGrid【英文标题】:Adding a Button to a WPF DataGrid 【发布时间】:2011-03-04 00:37:03 【问题描述】:我想在 WPF 中创建一个DataGrid
控件,其中每行的第一个单元格中都有一个按钮。单击此按钮将显示 RowDetailsTemplate
或 SubRow。
如何添加显示/隐藏RowDetailsTemplate
的按钮?
【问题讨论】:
【参考方案1】:首先创建一个DataGridTemplateColumn
来包含按钮:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="ShowHideDetails">Details</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
点击按钮时,更新包含DataGridRow
的DetailsVisibility
:
void ShowHideDetails(object sender, RoutedEventArgs e)
for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
if (vis is DataGridRow)
var row = (DataGridRow)vis;
row.DetailsVisibility =
row.DetailsVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
break;
【讨论】:
这会在第一列添加按钮,最后一列有什么想法吗? @Hassan Ansari 这会将按钮添加到您放置的任何位置。如果您将上面显示的 XAML 放在其他列之前,则该按钮将显示为第一列。如果将 XAML 放在所有其他列之后,则该按钮将显示为最后一列。如果你把它放在中间,按钮就会显示在中间。您可以按任意顺序放置列,并且按钮可以位于任意列中。 我的列是自动生成的,我在列中获取数据 datagrid.itemSource = List看看这个:
XAML:
<DataGrid Name="DataGrid1">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="ChangeText">Show/Hide</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
方法:
private void ChangeText(object sender, RoutedEventArgs e)
DemoModel model = (sender as Button).DataContext as DemoModel;
model.DynamicText = (new Random().Next(0, 100).ToString());
类:
class DemoModel : INotifyPropertyChanged
protected String _text;
public String Text
get return _text;
set _text = value; RaisePropertyChanged("Text");
protected String _dynamicText;
public String DynamicText
get return _dynamicText;
set _dynamicText = value; RaisePropertyChanged("DynamicText");
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(String propertyName)
PropertyChangedEventHandler temp = PropertyChanged;
if (temp != null)
temp(this, new PropertyChangedEventArgs(propertyName));
初始化代码:
ObservableCollection<DemoModel> models = new ObservableCollection<DemoModel>();
models.Add(new DemoModel() Text = "Some Text #1." );
models.Add(new DemoModel() Text = "Some Text #2." );
models.Add(new DemoModel() Text = "Some Text #3." );
models.Add(new DemoModel() Text = "Some Text #4." );
models.Add(new DemoModel() Text = "Some Text #5." );
DataGrid1.ItemsSource = models;
【讨论】:
这似乎与问题无关,该问题是关于单击按钮时如何显示/隐藏详细信息行。此代码的作用是在单击按钮时导致模型中的某些数据发生变化。【参考方案3】:XAML
<DataGrid x:Name="dgv_Students" AutoGenerateColumns="False"
ItemsSource="Binding People" Margin="10,20,10,0"
Style="StaticResource AzureDataGrid" FontFamily="B Yekan"
Background="#FFB9D1BA">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="RowButton_Click">Text</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
代码隐藏
private IEnumerable<DataGridRow> GetDataGridRowsForButtons(DataGrid grid)
//IQueryable
if (!(grid.ItemsSource is IEnumerable itemsSource))
yield return null;
foreach (var item in itemsSource)
var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (null != row & row.IsSelected)
yield return row;
private void RowButton_Click(object sender, RoutedEventArgs e)
for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
if (vis is DataGridRow)
// var row = (DataGrid)vis;
var rows = GetDataGridRowsForButtons(dgv_Students);
string id;
foreach (DataGridRow dr in rows)
id = (dr.Item as tbl_student).Identification_code;
MessageBox.Show(id);
break;
break;
单击按钮后,该行的 ID 会返回给您,您可以将其用作按钮名称。
【讨论】:
以上是关于将按钮添加到 WPF DataGrid的主要内容,如果未能解决你的问题,请参考以下文章
WPF开发为按钮提供添加,删除和重新排列ListBox内容的功能