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;
}
}
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;
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;
}
}
4条答案
按热度按时间cfh9epnr1#
首先创建一个
DataGridTemplateColumn
来包含按钮:单击按钮后,更新包含
DataGridRow
的DetailsVisibility
:hzbexzde2#
看看这个
XAML:
方法:
类别:
初始化编码:
nimxete23#
XAML
代码隐藏
单击按钮后,该行的ID将返回给您,您可以将其用作按钮名称。
l7wslrjt4#
如果你想使用
Command
,你可以使用下面的例子作为基础。检查
AncestorTyp
是否与ViewModel
绑定的父视图匹配。