【问题标题】:Datagrid Custom Combobox not UpdatingDatagrid自定义组合框未更新
【发布时间】:2014-03-24 14:10:25
【问题描述】:

我遇到了 WPF Datagrid 的 DataGridTemplateColumn 问题,并根据用户的交互更新了 ComboBox 上显示的项目。由于程序是数据库绑定的,一次从数据库中加载所有项目是没有意义的,而是使用实体框架来拉取用户在给定时刻的请求。

为了便于查看紧凑,下一个/上一个按钮位于组合框模板上,以及搜索功能,我遇到的问题是: 当模板化组合框位于数据网格中时,下一个/上一个不起作用。在数据网格之外,它们可以正常工作。

我什至尝试使用组合框存在的知识,并通过按名称搜索该组合框手动将其从可视树中拉出,拉出其与 itemssource 的绑定并告诉它进行更新。什么都没有发生。

这是我在下面的意思的图像:

一旦我选择了一个新的单元格,显示就会更新;但是,由于我捕获了它即将进入编辑模式并更新列表以包含所选项目(出于绑定目的,ComboBoxes 不希望有一个不在其项目集中的选定项目。)

您可以想象,这是一个相当大的问题。在找到解决方案之前,很难继续开发它。这是 Datagrids 的已知问题吗?如果是这样,我可能不得不自己动手。出于性能原因,我不想这样做,我不是 MVVM 专家,UI 看起来如此的唯一原因是它基本上是在自身内部定义自己。

【问题讨论】:

    标签: wpf binding wpfdatagrid


    【解决方案1】:

    我很笨,这就是问题所在:

    private static void UpdateComboDropdown(string comboboxName, DataGrid dataGrid)
    {
        /* *
         * I think CurrentItem is used in this instance because 
         * it's a single cell selection mode?
         * *
         * I CBA to check.
         * */
        var selectedRow = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.CurrentItem) as DataGridRow;
        if (selectedRow != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(selectedRow);
            if (presenter != null)
            {
    
                var visualComboBox = presenter.FindChild<ComboBox>(comboboxName);
                if (visualComboBox != null)
                {
                    var binding = visualComboBox.GetBindingExpression(ComboBox.ItemsSourceProperty);
                    if (binding != null)
                        binding.UpdateTarget();
                }
                var popupPart = visualComboBox.FindChild<Popup>("PART_Popup");
                if (popupPart != null && popupPart.Child != null)
                {
                    //Popup has one child, reports zero children, so: search on its child.
                    var previous = popupPart.Child.FindChild<Button>("PreviousButton");
                    if (previous != null)
                    {
                        var previousIsEnabledBinding = previous.GetBindingExpression(Button.IsEnabledProperty);
                        if (previousIsEnabledBinding != null)
                            previousIsEnabledBinding.UpdateTarget();
                    }
                    var next = popupPart.Child.FindChild<Button>("NextButton");
                    if (next != null)
                    {
                        var nextIsEnabledBinding = next.GetBindingExpression(Button.IsEnabledProperty);
                        if (nextIsEnabledBinding != null)
                            nextIsEnabledBinding.UpdateTarget();
                    }
                }
            };
        }
    }
    

    我告诉它更新组合框的 ItemsSource,而不是相反。至于为什么关系没有自动更新,我不知道。现在来了解如何在其下拉列表中更新组合框控件的组成元素。可能会涉及获取其 PART_Popup,然后是下一个/上一个按钮及其各自的绑定。这很烦人。

    编辑:用完整的解决方案更新了答案。

    【讨论】:

      猜你喜欢
      • 2010-12-04
      • 2011-04-30
      • 2015-12-17
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 2018-04-03
      相关资源
      最近更新 更多