wpf datagrid过滤列标题值访问

时间:2014-08-28 10:37:57

标签: wpf wpfdatagrid

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

[更多细节] 我正在使用我的一个wpf应用程序中的Filter控件.Downloaded项目还包含一个消费者测试项目(DataGridFilterTest)。在网格外部添加一个带有click事件的简单按钮(与网格无关).NOw我使用标题列文本框中的某些文本过滤数据。添加的按钮单击事件我想要这个文本框的值或对象。想法是我将这些文本保存在xml中的某个地方以及稍后的时间(新请求),我将打开带有相同文本的预固定数据过滤器的网格。

由于

2 个答案:

答案 0 :(得分:0)

像这样的方法,你通过VisualTree来获得所需的值。获得DataGridColumnHeaderDataGridColumnCell后很容易获得值:

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取了它,它对你非常有用

答案 1 :(得分:0)

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

在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来保存过滤器。

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事件(上图)。

希望这有帮助。