简体   繁体   English

在 WPF 中使用 ListView 的 GridView 时访问单元格文本

[英]Accessing cell text when using the GridView of a ListView in WPF

I am really struggling with the WPF ListView at the moment, I imagine a missing something really dumb but after a couple of hours of googling I though I had better ask.目前,我真的在为 WPF ListView 苦苦挣扎,我想我错过了一些非常愚蠢的东西,但经过几个小时的谷歌搜索后,我最好问问。

I am trying to access the text contents of individual cells in a ListView which has been created with a GridView thus:我正在尝试访问使用 GridView 创建的 ListView 中各个单元格的文本内容,因此:

<ListView Name="MyListView">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Foo" DisplayMemberBinding="{Binding Foo}"/>
            <GridViewColumn Header="Bar" DisplayMemberBinding="{Binding Bar}"/>
        </GridView>
    </ListView.View>
</ListView>

I am adding to the list view as so:我这样添加到列表视图中:

var foobars = new ObservableCollection<Foobar>();
foobars.Add(new Foobar { Foo = "Hello", Bar = "world" });
MyListView.ItemsSource = foobars;

Now I would like to grab the word "world" from the second column of the first row.现在我想从第一行的第二列中获取“世界”这个词。 If this was WinForms I would type:如果这是 WinForms,我会输入:

var word = (string)(((ListViewItem)MyListView.Items[0]).SubItems[1]);

I can't find any way to do this in WPF: I realize I could do:我在 WPF 中找不到任何方法:我意识到我可以做到:

var word = ((Foobar)MyListView.Items[0]).Bar;

But in my real application the text which appears in the ListView has been through one of a couple of ValueConverters based on which column it is in, and so is not the same as the text in the binding object.但在我的实际应用程序中,ListView 中显示的文本已通过基于它所在列的几个 ValueConverters 之一,因此与绑定 object 中的文本不同。 I just need to grab the text which is being displayed to the user, not the underlying data object.我只需要获取显示给用户的文本,而不是底层数据 object。

Do any of the code gurus here have any advice??这里的任何代码大师有什么建议吗?

Cheers,干杯,

Gavin加文

Ok, found a ugly way to do this.好的,找到了一个丑陋的方法来做到这一点。 Here is the code to get "world" from the first row, second column cell:这是从第一行第二列单元格中获取“世界”的代码:

var columns = ((GridView) MyListView.View).Columns;
var foobar = (Foobar)MyListView.Items[0];
var result = (string)DataBinder.Eval(foobar, (Binding) columns[0].DisplayMemberBinding);

This is using a helper class (thanks to http://social.expression.microsoft.com/Forums/en-US/wpf/thread/315d2442-b978-4e5f-89cd-1004a51f390d/ you saved alot of my hair from being pulled):这是使用一个助手 class (感谢http://social.expression.microsoft.com/Forums/en-US/wpf/thread/315d2442-b978-4e5f-89cd-1004a51f30的头发被拉了很多):

public class DataBinder
{
    private static readonly DependencyProperty BindingEvalProperty = DependencyProperty.RegisterAttached(
        "BindingEval",
        typeof(Object),
        typeof(DependencyObject),
        new UIPropertyMetadata(null));

    public static Object Eval(Object data, Binding binding)
    {
        var newbinding = new Binding {Path = binding.Path, Converter = binding.Converter, Source = data};
        var evalobj = new DependencyObject();
        BindingOperations.SetBinding(evalobj, BindingEvalProperty, newbinding);
        return evalobj.GetValue(BindingEvalProperty);
    }
}

So this is grabbing the binding object from the second column and evaluating it against the data object associated with the first row the same way the underlying GridView code would be.因此,这是从第二列中获取绑定 object 并根据与第一行关联的数据 object 对其进行评估,其方式与底层 GridView 代码相同。 This will work for my application, but if anyone has a neater solution please feel free to post an answer!这适用于我的应用程序,但如果有人有更简洁的解决方案,请随时发布答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM