简体   繁体   English

ItemsControl.ItemsSource 如何从没有实现 IEnumerable 的东西绑定?

[英]How does ItemsControl.ItemsSource bind from something that doesn't implement IEnumerable?

ItemsControl.ItemsSource is IEnumerable . ItemsControl.ItemsSourceIEnumerable DataTable isn't IEnumerable . DataTable不是IEnumerable I would expect DataTable to fail to bind to ItemsSource .我希望DataTable无法绑定到ItemsSource Yet, the code below works.然而,下面的代码有效。 Why?为什么?

<DataGrid ItemsSource="{Binding Table}"/>

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
}

public DataTable Table { get; set; } = new DataTable();

When I set ItemsSource to DataTable without binding, it fails like I would expect.当我在没有绑定的情况下将ItemsSource设置为DataTable ,它会像我预期的那样失败。 However, casting DataTable to IEnumerable<DataRowView> compiles without error.但是,将DataTableIEnumerable<DataRowView>编译时不会出错。

new DataGrid().ItemsSource = new DataTable(); // Cannot implicitly convert type 'System.Data.DataTable' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
new DataGrid().ItemsSource = (IEnumerable<DataRowView>)new DataTable(); // compiles without error

Does binding cast DataTable to IEnumerable<DataRowView> ?绑定是否将DataTableIEnumerable<DataRowView> Where is the documentation for this behavior?这种行为的文档在哪里? I would expect the ItemsSource documentation to explain this casting, but I don't see anything there.我希望ItemsSource文档能够解释这种转换,但我在那里什么也没看到。

First of all, the cast to IEnumerable<DataRowView> does not work.首先,转换为IEnumerable<DataRowView>不起作用。 It compiles, but will throw at runtime:它编译,但会在运行时抛出: 在此处输入图片说明

The binding works for DataTable because it implements IListSource绑定适用于DataTable因为它实现了IListSource

This interface is explicitly accounted for in the WPF DefaultValueConverter此接口在 WPF DefaultValueConverter明确说明

// special case for converting IListSource to IList
if (typeof(IListSource).IsAssignableFrom(sourceType) &&
                targetType.IsAssignableFrom(typeof(IList)))
{
    return new ListSourceConverter();
}

And the default converter is created with the binding并且默认转换器是使用绑定创建的

IValueConverter converter = Engine.GetDefaultValueConverter(type, TargetProperty.PropertyType, IsReflective);

暂无
暂无

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

相关问题 ItemsControl.ItemsSource,绑定无效 - ItemsControl.ItemsSource, binding doesn't work 如何将ItemsControl.ItemsSource与XAML中的属性绑定? - How can I bind an ItemsControl.ItemsSource with a property in XAML? 如何将ItemsControl的ItemsSource绑定到IEnumerable的T类内部的属性 <T> ? - How to bind ItemsControl's ItemsSource to a Property that is inside of class T from IEnumerable<T>? 显示ItemsControl.ItemsSource是否为null - Show if ItemsControl.ItemsSource is null 根据另一个 ItemsControl 的选定对象更新 ItemsControl.ItemsSource - Update ItemsControl.ItemsSource based on selected object of a another ItemsControl 如何绑定为ItemsControl的子级的ListBox的ItemsSource? - How to Bind ItemsSource of ListBox which is a Child of ItemsControl? 使用 ItemsSource 时操作无效。 改为使用 ItemsControl.ItemsSource 访问和修改元素 - Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead 使用ItemsSource时,操作无效。 在执行两次时,使用ItemsControl.ItemsSource访问和修改元素 - Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead when execution twice 使用ItemsSource时,操作无效。 使用ItemsControl.ItemsSource访问和修改元素 - Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource 可以从ItemsControl的数据模板绑定组合框Itemssource - Can one bind a combobox Itemssource from a datatemplate of a ItemsControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM