C# 如何使用WPF通过单击按钮选择整个DataGrid表行并将其删除?

C# 如何使用WPF通过单击按钮选择整个DataGrid表行并将其删除?,c#,.net,wpf,mvvm,datagrid,C#,.net,Wpf,Mvvm,Datagrid,我是WPF新手,有人能告诉我如何在MVVM模型中使用WPF选择DataGrid表行并通过单击按钮将其删除吗 我只能通过硬编码值来通过单击按钮删除行 HostSystemInformation info = (from sysinfo in systemInformation where sysinfo.Sno == 4 select sysinf

我是WPF新手,有人能告诉我如何在MVVM模型中使用WPF选择DataGrid表行并通过单击按钮将其删除吗

我只能通过硬编码值来通过单击按钮删除行

 HostSystemInformation info = (from sysinfo in systemInformation
                                     where sysinfo.Sno == 4 
                                     select sysinfo).First();
从上面的代码中,我只能删除第四行。我希望解决方案在我选择datagrid表中的一行时在变量中获取值。我想用那个变量代替硬编码的值4。 此编码不是在代码隐藏中完成的,而是在单独的ModalView文件中完成的

我已将我的代码复制到下面,有人为此提供了解决方案

XAML:

ModalView.csFile

private DelegateCommand deleteIp;

public DelegateCommand DeleteIp
{
    get { return deleteIp; }
    set { deleteIp = value; }
}

private ObservableCollection<HostSystemInformation> systemInformation;

public ObservableCollection<HostSystemInformation> SystemInformation
{
    get { return systemInformation; }
    set { SetProperty(ref systemInformation, value); }
}

public UserBase_ViewModal()
{
    SystemInformation = new ObservableCollection<HostSystemInformation>();
    deleteIp = new DelegateCommand(DeleteSystemInformationInIpTable);
}

private void DeleteSystemInformationInIpTable()
{
    try
    {        
        if(systemInformation.Count>0)
        {
            int count=0;
            foreach (object eno in systemInformation)
            {
                HostSystemInformation info = (from sysinfo in systemInformation
                                     where sysinfo.Sno == 4 
                                     select sysinfo).First(); /*Here instead of 4th row i need to pass variable dynamically by pressing any row */
                systemInformation.Remove(info);
                count++;
            }
        }
    }
    catch (Exception ex)
    {
        // MessageBox.Show(ex.Message);
    }
}

public class HostSystemInformation
{
    public int Sno { get; set; }
    public string strIpAddr { get; set; }
    public string strSystemName { get; set; }
    public string strStatus { get; set; }
}
这将从datagridview中选择的可观察集合中删除该项。这可能与可观察的集合有关

编辑:在保存datagrid的WPF类窗口中,添加

public static int index;
    private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        index = datagrid.SelectedIndex;
    }
然后,您可以从任何地方访问静态变量索引,以获得datagrid中的所选索引,如下所示。WPFclassname.index
希望这能解决您的问题

如果您使用MVVM,则不会删除DataGridRow。您要做的是-删除/删除Viewmodel中OberservableCollection中的绑定项。

感谢您的回复

我从我的朋友那里得到了一个非常简单的解决办法。我把它贴在下面

要在DataGrid表中选择行,有一个属性SelectedItem={Binding ItemSelect},不需要编写代码

视图:

问候,


R.Karthik

感谢您的回复,我不是在代码背后写代码,而是在单独的ModalView文件中写代码,因此我无法访问ModalView中的'Name=datagridIpTable'。哦。然后可以在WPF类中定义一个公共静态整数字段。双击datagrid,它将为更改的选择生成一个事件。在这种情况下,将datagridIpTable.SelectedIndex的值分配给静态字段。然后,您可以从Modal类访问公共字段,并从该索引处的可观察集合中删除该项。嗨,我被困在如何将值从代码隐藏传输到ModalView文件中。private void datagridIpTable_SelectionChangedobject sender,SelectionChangedEventArgs e{int x=datagridIpTable.SelectedIndex;}您正在事件中声明一个整数。在事件方法之外声明它,并且作为公共,我已经编辑了我的答案。
systeminformation.RemoveAt(datagridIpTable.SelectedIndex);
public static int index;
    private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        index = datagrid.SelectedIndex;
    }
<DataGrid Name="datagridIpTable"  ItemsSource="{Binding SystemInformation, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedSystemInformation}" AutoGenerateColumns="false" >
                <DataGrid.Columns >
                    <DataGridTextColumn Binding="{Binding Sno}"  Header="S.No" MinWidth="50" />
                    <DataGridTextColumn Binding="{Binding strSystemName}" Header="IP Address" MinWidth="240" />
                    <DataGridTextColumn Binding="{Binding strStatus}" Header="Status" MinWidth="140" />
                </DataGrid.Columns>
            </DataGrid>
    private HostSystemInformation selectedSystemInformation;
    public HostSystemInformation SelectedSystemInformation
    {
        get { return selectedSystemInformation; }
        set { SetProperty(ref selectedSystemInformation, value); }
    }


    public UserBase_ViewModal()
    {
        deleteIp = new DelegateCommand(DeleteSystemInformationInIpTable);
    }

    private void DeleteSystemInformationInIpTable()
    {
       SystemInformation.Remove(SelectedSystemInformation);
    }