`So I am trying to use the DataGrid and create a Column Template to add a delete button. The problem is that the row is databound to a record that in my observableCollection, but the binding of the button has to be bound to the viewmodel and not to the object of the row. if a datacontext is added to the button, then the button will fire. However the SelectedValue for the Grid that is bound to a property ends up null if that is done.
Here is the code.
< DataGrid x: Name = "ActualDataGrid" DockPanel.Dock = "Top"
ItemsSource = "{Binding ActualData}"
Margin = "5,10,5,5"
FontSize = "14"
FontWeight = "Bold"
Background = "WhiteSmoke"
AlternatingRowBackground = "AliceBlue"
VerticalAlignment = "Top"
HorizontalAlignment = "Stretch"
AutoGenerateColumns = "False"
GridLinesVisibility = "Horizontal"
ColumnHeaderHeight = "45"
SelectionMode = "Extended"
CanUserAddRows = "True"
CanUserDeleteRows = "False"
SelectedValue = "{Binding SelectedResult}"
Height = "{Binding ActualHeight, ElementName=ProjectedDataGrid}"
VerticalScrollBarVisibility = "Visible" >
<DataGrid.ColumnHeaderStyle >
<Style TargetType = "DataGridColumnHeader" >
<Setter Property = "ContentTemplate" >
<Setter.Value >
<DataTemplate >
<TextBlock TextWrapping = "Wrap" Text = "{Binding}" FontWeight = "Bold" ></ TextBlock >
</ DataTemplate >
</ Setter.Value >
</ Setter >
</ Style >
</ DataGrid.ColumnHeaderStyle >
<DataGrid.Columns >
< DataGridTextColumn Header = "Num" Width = "50" Binding = "{Binding Path= num}" />
< DataGridTextColumn Header = "Notes" Width = "*" Binding = "{Binding Path= description}" />
< DataGridTemplateColumn Header = "Delete" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate >
<Button Command = "{Binding DeleteCommand}"
Margin = "3" Background = "Transparent" BorderThickness = "0" >
<Button.DataContext > <=== Optional does not work because selected value nulled.
<future:VmModel x:Name = "VM" />
</ Button.DataContext >
<Button.Content >
<materialDesign:PackIcon Kind = "TrashCanOutline" VerticalAlignment="Center"/>
</Button.Content>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
DeleteCommand = new RelayCommand(param => CanDelete(param), param => Delete(param));
private bool CanDelete(object o)
{
return true;// _selectedResult != null;
}
private ICommand _deleteCommand;
public ICommand DeleteCommand
{
get
{
return _deleteCommand;
}
set
{
_saveCommand = value;
}
}
private void Delete(object result)
{
}
private FutureChartData _selectedResult;
public FutureChartData SelectedResult
{
get { return _selectedResult; }
set
{
_selectedResult = value;
OnPropertyChanged("SelectedResult");
}
}
< Button Command = "{Binding DeleteCommand}"
Margin = "3" Background = "Transparent" BorderThickness = "0" >
< Button.DataContext > <=== Optional does not work because selected value nulled.
<future:VmModel x:Name = "VM" />
</ Button.DataContext >
< Button.Content >
< materialDesign:PackIcon Kind = "TrashCanOutline" VerticalAlignment="Center"/>
</Button.Content>`
</Button>
New contributor
EagleSparrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.