【问题标题】:How to select an entire DataGrid Table Row and delete it with a button click using WPF?如何选择整个 DataGrid 表格行并使用 WPF 单击按钮将其删除?
【发布时间】:2015-01-06 11:09:39
【问题描述】:

我是 WPF 新手,谁能解释我如何在 MVVM 模型中使用 WPF 选择 DataGrid 表格行并通过单击按钮将其删除。

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

 HostSystemInformation info = (from sysinfo in systemInformation
                                     where sysinfo.Sno == 4 
                                     select sysinfo).First();

从上面的代码我只能删除第 4 行。当我在数据网格表中选择一行时,我想要在变量中获取值的解决方案。我想使用该变量而不是硬编码值 4。 此编码不是在代码隐藏中完成的,而是在单独的 ModalView 文件中完成的

我已经复制了我的代码,下面有人对此给出了解决方案。

XAML:

<Button Content="Remove" Command="{Binding DeleteIp}" Grid.Row="0" Grid.Column="1" FontFamily="Ebrima" FontSize="12" Width="61" Height="25" HorizontalAlignment="right" VerticalAlignment="center"/>
<DataGrid  Name="datagridIpTable"  ItemsSource="{Binding SystemInformation}"  SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}" AutoGenerateColumns="false"  Grid.Row="1" Grid.Column="1" CanUserAddRows="False" >
    <DataGrid.Columns >
        <DataGridTextColumn Binding="{Binding Sno}"  Header="S.No" MinWidth="50" />
        <DataGridTextColumn Binding="{Binding strIpAddr}" Header="System Name" MinWidth="240"/>
        <DataGridTextColumn Binding="{Binding strSystemName}" Header="IP Address" MinWidth="240"/>
        <DataGridTextColumn Binding="{Binding strStatus}" Header="Status" MinWidth="140" />
    </DataGrid.Columns>
</DataGrid>

ModalView.cs文件

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; }
}

【问题讨论】:

    标签: c# .net wpf mvvm datagrid


    【解决方案1】:
    systeminformation.RemoveAt(datagridIpTable.SelectedIndex);
    

    这将从数据网格视图中选择的可观察集合中删除项目。 (可能绑定到那个可观察的集合)

    编辑:在包含数据网格的 WPF 窗口类中,添加

    public static int index;
        private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            index = datagrid.SelectedIndex;
        }
    

    然后您可以从任何地方访问静态变量索引,以像这样在数据网格中获取选定的索引。 WPFclassname.index 希望这能解决你的问题

    【讨论】:

    • 感谢您的回复,我不是在代码隐藏中编写代码,而是在单独的 ModalView 文件中编写代码,因此我无法在 ModalView 中访问“Name =datagridIpTable”。
    • 哦。然后您可以在 WPF 类中定义一个公共静态整数字段。双击数据网格,它将生成一个用于选择更改的事件。在这种情况下,将 datagridIpTable.SelectedIndex 的值分配给静态字段。然后,您可以从 Modal 类访问公共字段,并从该索引处的可观察集合中删除该项目。
    • 嗨,我不知道如何将值从代码隐藏传输到 ModalView 文件。私人无效datagridIpTable_SelectionChanged(对象发送者,SelectionChangedEventArgs e){int x = datagridIpTable.SelectedIndex; }
    • 您在事件中声明一个整数。在事件方法之外声明它并公开(我已经编辑了我的答案)。
    【解决方案2】:

    如果你使用 MVVM,你会删除 DataGridRow。你要做的是 - 删除/移除 Viewmodel 中 OberservableCollection 中的绑定项目。

    【讨论】:

      【解决方案3】:

      感谢您的热情回复

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

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

      查看:

      <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);
          }
      

      问候,

      R.Karthik

      【讨论】:

        猜你喜欢
        • 2011-04-29
        • 1970-01-01
        • 1970-01-01
        • 2012-07-18
        • 2017-12-16
        • 1970-01-01
        • 2015-11-22
        • 1970-01-01
        相关资源
        最近更新 更多