gpt4 book ai didi

silverlight - WP7 - ListBox 绑定(bind)到嵌套的 ObservableCollection

转载 作者:行者123 更新时间:2023-12-04 02:49:20 24 4
gpt4 key购买 nike

我有一个 ObservableCollection 对象,如下所示:

public class UserDataViewModel
{
private ObservableCollection<CategoryItem> _data =
new ObservableCollection<CategoryItem>();

public ObservableCollection<CategoryItem> Data
{
get { return _data; }
private set { }
}
// Other methods to set Data
}

CategoryItem 类定义为:

public class CategoryItem : INotifyPropertyChanged
{
private string _name = null;
private ObservableCollection<EntryItem> _entries =
new ObservableCollection<EntryItem>();

public string Name
{
get { return _name; }
set {
if( value != _name ) {
_name = value;
NotifyPropertyChanged( "Name" );
}
}
}

public ObservableCollection<EntryItem> Entries
{
get { return _entries; }
set {
if( value != _entries ) {
_entries = value;
NotifyPropertyChanged( "Entries" );
}
}
}
// INotifyPropertyChanged code follows
}

EntryItem 类定义为:

public class EntryItem : INotifyPropertyChanged
{
private string _name = null;

public string Name
{
get { return _name; }
set {
if( value != _name ) {
_name = value;
NotifyPropertyChanged( "Name" );
}
}
}
// INotifyPropertyChanged code follows
}

我正在尝试将其绑定(bind)到 ListBox。每个 ListBoxItem 由 2 个 TextBlock 组成。我希望第一个 TextBlock 显示 EntryItem.Name 属性,第二个显示 CategoryItem.Name 属性。这是我在 XAML 中尝试过的(没有成功):

<ListBox x:Name="MyListBox"
Margin="0,0,-12,0"
ItemsSource="{Binding Data}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<!--This should display EntryItem.Name-->
<TextBlock Text="{Binding Entries.Name}"
TextWrapping="Wrap"
Margin="12,0,0,0"
Style="{StaticResource PhoneTextExtraLargeStyle}" />

<!--This should display CategoryItem.Name-->
<TextBlock Text="{Binding Name}"
TextWrapping="Wrap"
Margin="12,-6,0,0"
Style="{StaticResource PhoneTextSubtleStyle}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

在此页面的代码隐藏中,我正在设置:

DataContext = App.ViewModel; // ViewModel is of type UserDataViewModel

我不断收到绑定(bind)错误:

System.Windows.Data Error: BindingExpression path error: 'Name' property not found on 'System.Collections.ObjectModel.ObservableCollection`1[NestedCollection.ViewModels.EntryItem]' 'System.Collections.ObjectModel.ObservableCollection`1[NestedCollection.ViewModels.EntryItem]' (HashCode=123081170). BindingExpression: Path='Entries.Name' DataItem='NestedCollection.ViewModels.CategoryItem' (HashCode=121425257); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..

NestedCollection 是该项目的名称,以上所有类都在 NestedCollection.ViewModels 命名空间中。

仅显示第二个 TextBlock 的内容。我该如何解决这个问题?

感谢您的帮助,这已经让我发疯了几个小时了!

编辑:

假设 Data 集合有 2 个条目,“Credit Cards”和“Email Accounts”(这些是每个 CategoryItem 对象的 Name 属性在集合中。假设第一个 CategoryItemEntryItem 对象“Visa”、“Mastercard”和“American Express”,第二个 CategoryItem对象有 EntryItem 对象“GMail”和“Hotmail”,然后我希望 ListBox 显示:

Visa
Credit Cards

Mastercard
Credit Cards

American Express
Credit Cards

GMail
Email Accounts

Hotmail
Email Accounts

我意识到 DataEntries 属性没有 Name 属性,其中的每个条目都有。无论如何要索引到 XAML 绑定(bind)中的 Entries 吗?

最佳答案

您正在尝试绑定(bind) ObservableCollection<T>TextBox .想一想。

ObservableCollection<EntryItem>没有名为 Name 的属性. EntryItem上课。

我建议您使用 ItemsControl代替或使用 Converter转换 EntryItem名称转换成逗号分隔的字符串。

查看您的编辑后:

试试下面的代码:

<ListBox x:Name="MyListBox"
Margin="0,0,-12,0"
ItemsSource="{Binding Data}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Name="RootGrid">
<ItemsControl ItemsSource="{Binding Entries}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<!--This should display EntryItem.Name-->
<TextBlock Text="{Binding Name}"
TextWrapping="Wrap"
Margin="12,0,0,0"
Style="{StaticResource PhoneTextExtraLargeStyle}" />
<!--This should display CategoryItem.Name-->
<TextBlock Text="{Binding ElementName=RootGrid, Path=DataContext.Name}"
TextWrapping="Wrap"
Margin="12,-6,0,0"
Style="{StaticResource PhoneTextSubtleStyle}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

--编辑--

看起来这是 Silverlight 3 中的一个已知问题.

http://forums.silverlight.net/forums/p/108804/280986.aspx

要解决此问题,请添加 CategoryItem 的引用在 EntryItem命名为 Parent或类似的东西来访问它。

public class EntryItem : INotifyPropertyChanged
{
public CategoryItem Parent { get; set; }
....
}

或者如上面链接中所述,将您的 DataTemplate变成 UserControl让它工作。

关于silverlight - WP7 - ListBox 绑定(bind)到嵌套的 ObservableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4622916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com