Member 14543386
Hi everyone
I have a datagrid, which I linked to a list as a source, and in a column I put a button to delete the row until the user clicked on that row to delete it.
But I get the following error !!
Thanks for the guidance everyone
код xaml-это :
<pre> <DataGrid VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" HorizontalAlignment="Center" IsEnabled="{Binding ElementName=chkitems, Path=IsChecked}" Height="266" x:Name="dgvitems" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" IsReadOnly="False" Width="451">
<DataGrid.Columns>
<DataGridTextColumn Header="نام کالا" Width="250" FontSize="14" Binding="{Binding name}"/>
<DataGridTextColumn Header="تعداد" Width="80" FontSize="14" Binding="{Binding number}"/>
<DataGridTemplateColumn Header=" ">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Button x:Name="btndelete" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="{x:Null}" Click="Btndelete_Click" ToolTip="حذف رکورد" Background="{x:Null}">
<materialDesign:PackIcon Kind="CloseCircle" Foreground="Red" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
а список ми есть :
<pre lang="c#">
public class datagridset
{
public string name { get; set; }
public int number { get; set; }
};
-----
<pre> private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<datagridset> datagridsets = new List<datagridset>();
dgvitems.ItemsSource = datagridsets.ToList();
}
------
private void Btndelete_Click(object sender, RoutedEventArgs e)
{
if(dgvitems.SelectedItem!=null)
{
dgvitems.Items.Remove(dgvitems.SelectedItem);
}
}