【问题标题】:C# WPF DataGrid Editable ComboBox Returns -1 as Selected Row With Single-Click-EditC# WPF DataGrid 可编辑组合框返回 -1 作为单击编辑的选定行
【发布时间】:2018-08-24 17:24:21
【问题描述】:

我正在尝试从 DataGrid 表中的可编辑组合框中获取单元格值。从选项中选择项目时成功/正确提取值,但当用户在组合框中输入文本时不会(除非他们双击)

问题:RowIndex 变为-1(在单击文本条目时),好像没有选择一行,导致代码失败,并错误地处理更新。

我该如何解决这个问题?如果强制用户双击是一个有效的选项,我该怎么做?

这是我的 C# 代码:

private void ComboBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (e != null)
            {
                TextBox t = e.Source as TextBox;
                if (t != null)
                {
                    try
                    {
                        int RowIndex = MYGRID.Items.IndexOf(MYGRID.SelectedItem);
                        if (RowIndex < 0)
                        {
                            MessageBox.Show("Index < 0"); //For Testing
                        }
                        //Obtain new Value
                        string Value = t.Text.ToString();
                        //Obtain item ID
                        DataGridRow Row = (DataGridRow)MYGRID.ItemContainerGenerator.ContainerFromIndex(RowIndex);
                        DataGridCell RowColumn = MYGRID.Columns[0].GetCellContent(Row).Parent as DataGridCell;
                        int ID = Convert.ToInt32(((TextBlock)RowColumn.Content).Text);

                        //Unrelated code continues...

                        }
                        catch (Exception) { }
                    }
                }

            }

这是我的列的 XAML 代码:

        <DataGridTemplateColumn x:Name="ValueCol" Header="Value">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox  ItemsSource="{Binding Options, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=UpdatedData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True}" IsEditable="True" LostFocus="ComboBox_LostFocus"  />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

【问题讨论】:

  • 我刚刚测试了你的情况,我可以确认网格上的选定项目没有被设置。您是否尝试过使用VisualTreeHelper 来获取DataGridRow 的父类型?
  • 我该怎么做?
  • google wpf find parent 你会发现一个特定类型的父母的recursive搜索,一旦你拥有它然后设置选定的属性。然后您的绑定将启动,并且网格中选定项目的属性将具有当前选定的项目。

标签: c# wpf combobox datagrid


【解决方案1】:

不确定您在做什么,但如果您只想阅读ComboBox 中的Text,请使用发件人老兄!

private void ComboBox_LostFocus(Object sender, RoutedEventArgs e)
{
    var comboBox = sender as ComboBox;
    if (comboBox != null)
    {
        int id;
        if(int.TryParse(comboBox.Text, out id))
        {
            // do your thing!!!!
        }
    }
}

要获取索引,您可以使用以下辅助方法:

public static DependencyObject GetParent<T>(DependencyObject child)
{
    if (child == null) return null;

    var parent = VisualTreeHelper.GetParent(child);
    return parent is T ? parent : GetParent<T>(parent);
}

用法

var index = ((DataGridRow)GetParent&lt;DataGridRow&gt;(comboBox))?.GetIndex();

【讨论】:

  • 我刚刚尝试实现该方法,但 TryParse 在单击时失败,但在双击时没有
  • 可能是因为你的组合框文本不是整数值!
  • 我可以通过 Value = comboBox.Text; 获得字符串值在这种情况下,有没有办法获取当前行的整数值?我仍然需要行索引来从其他列中获取其他值。
  • 好的,我们到了!我刚才需要获取同一行的两个不同列中的值,但是我之前做的方法是使用索引作为 DataGridRow 对象,并且不再起作用,因为它没有引用同一个对象。
  • 不知道你是什么意思,但如果你使用DaraGridRow befire 那么,你可以使用与以前相同的方法而不会出现任何问题。
猜你喜欢
  • 2013-11-03
  • 1970-01-01
  • 2015-10-07
  • 2011-03-26
  • 2020-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多