【问题标题】:wpf datagrid filter column header value accesswpf datagrid过滤器列标题值访问
【发布时间】:2014-10-22 04:39:27
【问题描述】:

我正在使用来自http://www.codeproject.com/Articles/42227/Automatic-WPF-Toolkit-DataGrid-Filtering 的控制数据网格过滤器控件,它工作正常。 我想保存过滤条件(标题文本框值)。 如何在任何按钮单击时获取标题/文本框值,并在其他事件中再次设置一些标题文本框。

[更多详情] 我在我的一个 wpf 应用程序中使用过滤器控件。下载的项目还包含一个消费者测试项目 (DataGridFilterTest)。在网格外添加一个带有单击事件的简单按钮(与网格无关)。现在我在标题列文本框中使用一些文本过滤数据。添加的按钮单击事件我想要此文本框的值或对象。想法是我将这些文本保存在 xml 中的某处,稍后下次(新请求),我将使用具有相同文本的预固定数据过滤器打开网格。

谢谢

【问题讨论】:

    标签: wpf wpfdatagrid


    【解决方案1】:

    这样的方法真是太棒了,您可以通过VisualTree 获得所需的值。一旦你有了DataGridColumnHeader 和DataGridColumnCell 就很容易获得价值:

    private void DataGrid_MouseRightButtonUp(object sender,
                                                      MouseButtonEventArgs e)
    {
        DependencyObject dep = (DependencyObject)e.OriginalSource;
    
        // iteratively traverse the visual tree
        while ((dep != null) &&
                !(dep is DataGridCell) &&
                !(dep is DataGridColumnHeader))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }
    
        if (dep == null)
            return;
    
        if (dep is DataGridColumnHeader)
        {
            DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
            // do something
        }
    
        if (dep is DataGridCell)
        {
            DataGridCell cell = dep as DataGridCell;
            // do something
        }
    }
    

    我是从http://www.scottlogic.com/blog/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html这里拿来的,它对你很有用

    【讨论】:

    • 感谢 Juan,我可以通过单击按钮下的 datagrid.Columns 或 ColumnHeaderStyle 属性获取 datagrid 列标题,但无法获取文本框和其他过滤器控件(日期时间)对象及其属性。
    【解决方案2】:

    我遇到了完全相同的问题。这是一个解决方案。它可能不是最优雅的,但它确实有效。

    在 Datagrid 的 Loaded 事件中,找到 TextBox 元素。你可以使用写成here的算法。您可以通过检查它们的名称来缩小元素范围,这将是“PART_TextBoxFilter”,它们的类型将是 DataGridFilterLibrary.Support.DelayTextBox(它是 TextBox 的后代)。它仍然会发现比您需要的更多,您可以检查它们的绑定路径是否不为空(这不是以下示例的一部分)。关键是要在它们上挂一个 KeyDown 事件处理程序:

    void dgFilter_Loaded(object sender, RoutedEventArgs e)
    {
        foreach (TextBox tb in FindVisualChildren<TextBox>(sender as DataGrid))
        {
            if (tb != null && tb.Name == "PART_TextBoxFilter")
            {
                tb.KeyUp += new KeyEventHandler(tbDelayTextBox_KeyUp);
            }
        }
    }
    

    在事件处理方法中,可以达到过滤器值,但这还不够。绑定路径的名称也必须保存,否则您将不知道它应该过滤哪一列。此外,您的窗口中可以有多个数据网格,因此数据网格的名称也很重要。 我使用字典来保存过滤器。

    Dictionary<string,string> filterValues;
    void tbDelayTextBox_KeyUp(object sender, KeyEventArgs e)
    {
        string dgName;
        var bindingPath = DelayTextBoxBindingPath(sender as DataGridFilterLibrary.Support.DelayTextBox, out dgName);
        if (!String.IsNullOrEmpty(bindingPath))
        {
            var key = dgName + "_" + bindingPath;
            if (filterValues.ContainsKey(key))
            {
                filterValues[key] = ((TextBox)sender).Text;
            }
            else
            {
                filterValues.Add(key, ((TextBox)sender).Text);
            }
        }
    }
    

    这里是获取绑定路径和数据网格名称的方法:

    string DelayTextBoxBindingPath(DataGridFilterLibrary.Support.DelayTextBox __dtb, out string datagridName)
    {
        datagridName = String.Empty;
        var result = String.Empty;
    
        if (__dtb != null)
        {
            BindingExpression be = __dtb.GetBindingExpression(TextBox.TextProperty) as BindingExpression;
            if (be != null)
            {
                var dgcf = be.DataItem as DataGridFilterLibrary.DataGridColumnFilter;
                if (dgcf != null && dgcf.FilterCurrentData != null)
                {
                    result = dgcf.FilterCurrentData.ValuePropertyBindingPath;
                    datagridName = dgcf.DataGrid.Name;
                }
            }
        }
    
        return result;
    }
    

    要保存/加载过滤器,您可以分别使用窗口的 OnClosing 和 Datagrid 的 Loaded 事件(上图)。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-11-25
      • 2012-11-18
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多