嵌套集合LLS ItemsSource绑定与分组

时间:2014-12-08 02:05:57

标签: c# xaml silverlight data-binding observablecollection

我试图将名为ObservableCollection的{​​{1}}自定义对象绑定到LongListSelector,该对象本身包含ItemGroupObservableCollection个对象(以及其他属性) (在WP8.1 Silverlight中创建Item个对象的分组:

Item

ViewModel包含属性:

public class Item : INotifyPropertyChanged {
    private string _name = "";
    public string Name {
        get {
            return _name;
        }
        set {
            if (value != _name) {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    private int _cost = 0;
    public int Cost {
        get {
            return _cost;
        }
        set {
            if (value != _cost) {
                _cost = value;
                NotifyPropertyChanged("Cost");
            }
        }
    }
}

public class ItemGroup : INotifyPropertyChanged {
    private string _groupName = "";
    public string GroupName {
        get {
            return _groupName;
        }
        set {
            if (value != _groupName) {
                _groupName = value;
                NotifyPropertyChanged("GroupName");
            }
        }
    }

    private int _totalCost = 0;
    public int TotalCost {
        get {
            return _totalCost;
        }
        set {
            if (value != _totalCost) {
                _totalCost = value;
                NotifyPropertyChanged("TotalCost");
            }
        }
    }

    private ObservableCollection<Item> _items = new ObservableCollection<Item>();
    public ObservableCollection<Item> Items {
        get {
            return _items;
        }
        set {
            if (value != _items) {
                _items = value;
                NotifyPropertyChanged("Items");
            }
        }
    }
}

和XAML:

private ObservableCollection<ItemGroup> _itemGroupList = new ObservableCollection<ItemGroup>();
public ObservableCollection<ItemGroup> ItemGroupList {
    get {
        return _itemGroupList;
    }
    set {
        if (value != _itemGroupList) {
            _itemGroupList = value;
            NotifyPropertyChanged("ItemGroupList");
        }
    }
}

哪个不起作用。 <phone:LongListSelector ItemsSource="{Binding ItemGroupList}"> <!-- Template for ItemGroup Items --> <phone:LongListSelector.ItemTemplate> <DataTemplate> <Grid Height="Auto" Margin="12,0,2,15"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <!-- Item Name --> <TextBlock Text="{Binding Items.Name}" Grid.Column="0"/> <!-- Item Cost --> <TextBlock Text="{Binding Items.Cost, StringFormat='{}{0} dollars'}" Grid.Column="1"/> </Grid> </DataTemplate> </phone:LongListSelector.ItemTemplate> <!-- Template for ItemGroup headers --> <phone:LongListSelector.GroupHeaderTemplate> <DataTemplate> <Grid Background="#FFA2A2A2" Margin="0,10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <!-- ItemGroup Group Name --> <TextBlock Grid.Column="0" Text="{Binding GroupName}"/> <!-- ItemGroup Total Cost --> <TextBlock Grid.Column="1" Text="{Binding TotalCost}"/> </Grid> </DataTemplate> <phone:LongListSelector.GroupHeaderTemplate> </phone:LongListSelector> ItemGroupGroupName)的直接属性正确绑定且可见,但未显示TotalCost个对象!我怀疑这是因为我传递给Item错误的来源?我已尝试通过ItemTemplate直接在LLS中绑定Collection,但这也不起作用。

更新:将页眉控件绑定到ItemSource={Binding ItemGroupList.Items}会产生Items.Count ItemItems个实际ItemGroup个对象的数量,这表示存在确实存在每个Item的{​​{1}}个对象列表。

1 个答案:

答案 0 :(得分:1)

请注意,您在那里有一个嵌套集合,因此您需要2项控件。你无法绑定到&#34; Items.Name&#34;或&#34; Items.Cost&#34;因为那些不是属性。你可以使用&#34; Items [0] .Name&#34;,但我想你可能想要列出所有项目,比如这样(内部&#34; LongListSelector.ItemTemplate&#34;):

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <!-- Item Name  -->
                <TextBlock Text="{Binding Items.Name}" Grid.Column="0"/>

                <!-- Item Cost -->
                <TextBlock Text="{Binding Items.Cost, StringFormat='{}{0} dollars'}" Grid.Column="1"/>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>