C# Silverlight - 与 ObservableCollections 绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/712398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Silverlight - Binding with ObservableCollections
提问by JubJub
This is no doubt a newbish question, but I have looked for an answer to no avail. My setup is simple: I have a ListBox control defined in XAML and an ObservableCollection<MyClass>
in the same class. I am binding the ObservableCollection<MyClass>
to the ListBox.
这无疑是一个新手问题,但我一直在寻找无济于事的答案。我的设置很简单:我有一个在 XAML 中定义的 ListBox 控件和一个ObservableCollection<MyClass>
在同一个类中。我将绑定ObservableCollection<MyClass>
到列表框。
Within the hierarchy of this ListBox in XAML, I want to bind to a given MyClass object, not to a child property of the MyClass object.
在 XAML 中此 ListBox 的层次结构中,我想绑定到给定的 MyClass 对象,而不是绑定到 MyClass 对象的子属性。
To clarify, I have XAML that looks like the following (I bind the ObservableCollection in code):
为了澄清起见,我有如下所示的 XAML(我在代码中绑定了 ObservableCollection):
<ListBox x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<MyControls:SpecialControl MyClassObj="{Binding !!!}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Surely there is a way to get at the object of an ObservableCollection rather than being forced to bind to one of its child properties.
当然,有一种方法可以获取 ObservableCollection 的对象,而不是被迫绑定到其子属性之一。
采纳答案by dtb
You do not have to specify a Path
if you want to use the bound object itself:
Path
如果要使用绑定对象本身,则不必指定 a :
<ListBox x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<MyControls:SpecialControl MyClassObj="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
BTW: Instead of your custom property, you can use the DataContext
property of your control to bind the control to the object:
顺便说一句:您可以使用DataContext
控件的属性将控件绑定到对象,而不是您的自定义属性:
<ListBox x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<MyControls:SpecialControl DataContext="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
回答by sipwiz
As well as specifying the binding path in your XAML you have to bind your collection to the ListBox.
除了在 XAML 中指定绑定路径外,您还必须将集合绑定到 ListBox。
C#
C#
ObservableCollection<MyClass> myCollection = new ObservableCollection<MyClass>();
MyListBox.DataContext = myCollection;
The XAML you have used won't be particularly useful unless you have overriden the ToString method on MyClass. Even though you say you're not are you sure it's not a property of MyClass that you want to bind to? I can't see why you'd want to bind directly to a collection object.
除非您覆盖了 MyClass 上的 ToString 方法,否则您使用的 XAML 不会特别有用。即使你说你不是你确定它不是你想要绑定的 MyClass 的属性吗?我不明白您为什么要直接绑定到集合对象。
XAML
XAML
<ListBox x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<MyControls:SpecialControl MyClassObj="{Binding Path=MyClassProperty}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>