简体   繁体   English

WPF绑定路径= /不工作?

[英]WPF Binding Path=/ not working?

I've set up my DataContext like this: 我已经设置了我的DataContext如下所示:

<Window.DataContext>
    <c:DownloadManager />
</Window.DataContext>

Where DownloadManager is Enumerable<DownloadItem> . 其中DownloadManagerEnumerable<DownloadItem> Then I set my DataGrid like this: 然后我像这样设置我的DataGrid

<DataGrid Name="dataGrid1" ItemsSource="{Binding Path=/}" ...

So that it should list all the DownloadItems, right? 所以它应该列出所有的DownloadItems,对吗? So I should be able to set my columns like: 所以我应该能够设置我的列:

<DataGridTextColumn Binding="{Binding Path=Uri, Mode=OneWay}"

Where Uri is a property of the DownloadItem . 其中UriDownloadItem的属性。 But it doesn't seem to like this. 但它似乎并不喜欢这样。 In the visual property editor, it doesn't recognize Uri is a valid property, so I'm guessing I'm doing something wrong. 在可视属性编辑器中,它不识别Uri是一个有效的属性,所以我猜我做错了。

It was working before, when I had the data grid binding to Values , but then I took that enumerable out of the DownloadManager and made itself enumerable. 它之前有工作,当我将数据网格绑定到Values ,然后我将这个可枚举的数据从DownloadManager取出并使其自身可枚举。 How do I fix this? 我该如何解决?

PS: By "doesn't work" I mean it doesn't list any items. PS:“不起作用”我的意思是它没有列出任何项目。 I've added some to the constructor of the DM, so it shouldn't be empty. 我已经在DM的构造函数中添加了一些,所以它不应该是空的。

Try ItemsSource="{Binding}" . 尝试ItemsSource="{Binding}" It should be enough. 这应该够了。

In response to the OP's question of why {Binding} works, but {Binding Path=/} does not work, I'm adding the following info as an answer to clarify the difference. 为了回应OP关于为什么{Binding}工作的问题,但是{Binding Path = /}不起作用,我将添加以下信息作为澄清差异的答案。

The following is taken from MSDN Data Binding Overview > Binding to Collections : 以下内容摘自MSDN数据绑定概述>绑定到集合

Current Item Pointers 当前项目指针

Views also support the notion of a current item. 视图还支持当前项目的概念。 You can navigate through the objects in a collection view. 您可以在集合视图中浏览对象。 As you navigate, you are moving an item pointer that allows you to retrieve the object that exists at that particular location in the collection. 在导航时,您正在移动项目指针,该指针允许您检索存在于集合中该特定位置的对象。 For an example, see How to: Navigate Through the Objects in a Data CollectionView . 有关示例,请参见如何:在Data CollectionView中浏览对象

Because WPF binds to a collection only by using a view (either a view you specify, or the collection's default view), all bindings to collections have a current item pointer. 因为WPF仅通过使用视图(您指定的视图或集合的默认视图)绑定到集合,所以对集合的所有绑定都具有当前项指针。 When binding to a view, the slash ("/") character in a Path value designates the current item of the view. 绑定到视图时,Path值中的斜杠(“/”)字符指定视图的当前项。 In the following example, the data context is a collection view. 在以下示例中,数据上下文是集合视图。 The first line binds to the collection. 第一行绑定到集合。 The second line binds to the current item in the collection. 第二行绑定到集合中的当前项。 The third line binds to the Description property of the current item in the collection. 第三行绑定到集合中当前项的Description属性。

<Button Content="{Binding }" />
<Button Content="{Binding Path=/}" />
<Button Content="{Binding Path=/Description}" />

As an alternative sol'n to Danko's answer, I discovered Static Resources! 作为Danko答案的替代解决方案,我发现了静态资源! (I'm a WPF noob) (我是WPF菜鸟)

<Window x:Class="ImageGetGUI.MainWindow"
    ...
    <Window.Resources>
        <c:DownloadManager x:Key="dm"/>
    </Window.Resources>
    ...
    <DataGrid Name="dataGrid1" ItemsSource="{StaticResource dm}" ...

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

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