【问题标题】:wpf get selected row when checkbox value changes当复选框值更改时,wpf 获取选定的行
【发布时间】:2018-10-25 19:37:45
【问题描述】:

我正在用 c# 构建一个 wpf 应用程序。我有一个包含信息的 GridView。每行都有一个复选框。单击复选框时,我想接收该行的用户名值(在第 1 列中)。

现在它正在工作,但必须选择整行。否则我当然会收到一个空异常。

private void CheckBox_breakfast(object sender, RoutedEventArgs e)
{
    Reservation reservation = gridReservations.SelectedItem as Reservation;
    string name = reservation.user_name;
}

如何只选择复选框而不是整行来实现此功能?

已经在网上搜索并尝试了很多,但没有任何效果。

对我有很大帮助!

【问题讨论】:

  • 不相关,但您可能需要考虑使用 mvvm 模式来利用 wpf 潜力。
  • 为什么不能使用checked 事件处理程序来执行此操作?
  • 是的,已经这样做了。但是如何获取行的 id ?
  • 我遇到了类似的事情并想出了一个解决方法。我使行不可编辑,直到用户右键单击并从上下文菜单中选择“编辑”。这使您可以捕获该行,启用该行进行编辑,您还可以提供其他有用的上下文功能,如“删除”等。

标签: c# wpf checkbox


【解决方案1】:

您可以尝试将 CheckBox 本身的 DataContext 转换为 Reservation:

private void CheckBox_breakfast(object sender, RoutedEventArgs e)
{
    CheckBox checkBox = sender as CheckBox;
    Reservation reservation = checkBox.DataContext as Reservation;
    string name = reservation.user_name;
}

如果您没有将 CheckBox 的 DataContext 或其在单元格中的任何父元素显式设置为其他内容,这应该可以工作。

【讨论】:

    【解决方案2】:

    我刚刚遇到了与您相同的问题,并找到了解决方案。

    首先,如果您指的是 WPF(而不是 ASP Web 框架),那么您可能指的是 Datagrid 控件而不是 GridView 控件。

    这进入 XAML

    <DataGrid>
        <DataGrid.Columns>
           ... <!--other definitions of datagrid columns go here-->
           <DataGridCheckBoxColumn Header="Select to delete">
                <DataGridCheckBoxColumn.ElementStyle>
                    <Style> <!-- used the following for design sake to center vertically and horizontally the checkboxes relative to the other content in the datagrid-->
                        <Setter Property="TextBlock.VerticalAlignment" Value="Center" />
                        <Setter Property="TextBlock.HorizontalAlignment" Value="Center" />
                    </Style>
                </DataGridCheckBoxColumn.ElementStyle>
            </DataGridCheckBoxColumn>
        </DataGrid.Columns>
    </DataGrid>
    

    这应该放在后面的 .xaml.cs 代码中

    private void getCodMatricol_CheckBox_Selected_in_Datagrid()
        {
            List<int> your_list_of_items_that_correspond_to_checked_checkboxes = new List<int>();
            for (int i = 0; i < datagridGrupeProductie.Items.Count; i++)
            {
                var item = datagridGrupeProductie.Items[i];
                var mycheckbox = datagridGrupeProductie.Columns[10].GetCellContent(item) as CheckBox;
                var myTextBlock = datagridGrupeProductie.Columns[0].GetCellContent(item) as TextBlock;
                if ((bool)mycheckbox.IsChecked)
                {
                   your_list_of_items_that_correspond_to_checked_checkboxes.Add(int.Parse(myTextBlock.Text));
                }
            }
        }
    

    在我的示例中,我提取了数据网格中第一列的内容,其中包含一些由整数表示的代码。

    注意:数据网格中的行和列的索引从 [0] 开始(实际上与 C# 中的大多数索引一样)

    如果datadgrid中的列定义为(常用)

    </DataGridTextColumn> </DataGridTextColumn>
    

    然后它承载一个 TextBlock 元素。

    var myTextBlock = datagridGrupeProductie.Columns[0].GetCellContent(item) as TextBlock;
    

    您必须处理此控件的 Text 属性才能读取其内容并根据需要进行转换。

    int.Parse(myTextBlock.Text
    

    接下来,您将使用要提取的内容填充 List 集合:

    your_list_of_items_that_correspond_to_checked_checkboxes.Add(int.Parse(myTextBlock.Text));
    

    此外,如果您想对该值进行任何操作,则必须遍历集合

    foreach (var item in your_list_of_items_that_correspond_to_checked_checkboxes)
                {
                    //do whatever needed on each item
                }
    

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      相关资源
      最近更新 更多