简体   繁体   English

使用带有转换器的数据网格收集匿名类型

[英]Collection of Anonymous Types using a datagrid with converter

Is it possible to use a converter to return .ToList() of a collection anonymous types bound to the Itemssource of a Datagrid ? 是否可以使用转换器返回绑定到DatagridItemssource的集合匿名类型的.ToList()

I have tried this: 我已经试过了:

//converter //转换器

 class AnonymousTypeToListConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((ObservableCollection<Object>)value).ToList();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

//Datagrid Declaration in xaml // xaml中的Datagrid声明

<DataGrid ItemsSource="{Binding SelectionData, Converter={StaticResource anonConverter}}" AutoGenerateColumns="True"/>

This doesn't seem to work - the Converter gets called but my Datagrid is blank: 这似乎不起作用-调用了Converter ,但是我的Datagrid是空白的:

在此处输入图片说明

My reason for this is because Datagrid doesn't like anonymous types...as defined here but I don't think this approach really works for us using an MVVM approach... 我这样做的原因是因为Datagrid不喜欢匿名类型...如此Datagrid定义但我认为使用MVVM方法对我们来说这种方法真的不起作用...

The comments are so hard to read, so I'll continue in an answer. 这些评论很难阅读,因此我将继续回答。

What he meant to say was that he could not use an IEnumerable as the DataContext and so he chose the option to call ToList() to turn it into a List . 他的意思是说他不能使用IEnumerable作为DataContext ,因此他选择了调用ToList()的选项将其转换为List As I showed you in my last comment, he could just as easily have passed the IEnumerable into the ObservableCollection<T> constructor: 正如我在上一条评论中向您展示的那样,他可以轻松地将IEnumerable传递给ObservableCollection<T>构造函数:

DataContext = new ObservableCollection(
    list.Select(v => new { id = v, name = "name " + v })); 

There's no point in you casting your ObservableCollection<Location> into an ObservableCollection<Object> like you are doing in your Converter . 像在Converter一样,将ObservableCollection<Location>强制转换为ObservableCollection<Object>毫无意义。 I still don't get what it's for anyway, but you'd be better off doing this: 无论如何我还是一无所获,但是最好这样做:

public object Convert(object value, Type targetType, object parameter, 
CultureInfo culture)
{
    return new ObservableCollection<Location>((ObservableCollection<Location>)value);
}

Of course, there's no point in casting an ObservableCollection<Location> into an ObservableCollection<Location> and then passing it into a new ObservableCollection<Location> but I'm guessing that you'll actually be doing something else in there. 当然,将ObservableCollection<Location>强制转换为ObservableCollection<Location> ,然后将其传递给新的ObservableCollection<Location>但是我猜测您实际上将在其中执行其他操作。

If you tell us what you're actually trying to achieve with your Converter , then maybe you'd get some better answers? 如果您告诉我们您实际上要使用Converter实现的目标,那么也许您会得到一些更好的答案?

Objects with an anonymous type are internal. 具有匿名类型的对象是内部的。 You can bind to properties of anonymous types only when running in full trust. 只有在完全信任的情况下运行,才能绑定到匿名类型的属性。

In general, Internet applications should be restricted from having direct access to critical system resources, to prevent malicious damage. 通常,应限制Internet应用程序直接访问关键系统资源,以防止恶意破坏。 By default, HTML and client-side scripting languages are not able to access critical system resources. 默认情况下,HTML和客户端脚本语言无法访问关键的系统资源。 Because Windows Presentation Foundation (WPF) browser-hosted applications can be launched from the browser, they should conform to a similar set of restrictions. 由于可以从浏览器启动Windows Presentation Foundation(WPF)浏览器托管的应用程序,因此它们应符合一组类似的限制。 To enforce these restrictions, WPF relies on both Code Access Security (CAS) and ClickOnce. 为了实施这些限制,WPF同时依赖于代码访问安全性(CAS)和ClickOnce。

To cut the story short, WPF runs with partial trusted security and therefore Binding doesn't work with anonymous types by default. 简而言之,WPF使用部分受信任的安全性运行,因此默认情况下,绑定不适用于匿名类型。

However I bet there are "workarounds" on the internet. 但是我敢打赌,互联网上有“解决方法”。 :) :)

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

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