【问题标题】:Nested collection LLS ItemsSource binding with Grouping嵌套集合 LLS ItemsSource 与 Grouping 绑定
【发布时间】:2015-02-05 15:44:36
【问题描述】:

我正在尝试将名为ItemGroup 的自定义对象的ObservableCollection 绑定到LongListSelector(以创建Item 对象的分组),该对象本身包含Item 对象的ObservableCollection ) 在 WP8.1 Silverlight 中:

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");
            }
        }
    }
}

ViewModel 包含以下属性:

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

还有 XAML:

<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>

这不起作用。 ItemGroup 的直接属性(GroupNameTotalCost)正确绑定并且可见,但没有显示 Item 对象!我怀疑这是因为我传递给ItemTemplate 错误的来源?我已经尝试通过ItemSource={Binding ItemGroupList.Items} 直接在 LLS 中绑定 Collection,但这也不起作用。

更新: 将 Header 控件绑定到 Items.Count 会产生 ItemsItemGroupItem 对象的实际数量,这表明确实存在 Item 的列表每个 ItemGroup 的对象。

【问题讨论】:

    标签: c# xaml silverlight data-binding observablecollection


    【解决方案1】:

    请注意,您在那里有一个嵌套集合,因此您需要 2 个项目控件。您不能绑定到“Items.Name”或“Items.Cost”,因为它们不是属性。您可以使用“Items[0].Name”,但我想您可能想要列出所有项目,如下所示(在“LongListSelector.ItemTemplate”内):

    <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>
    

    【讨论】:

    • 谢谢!我想我试图避免嵌套控件,因为它可能违反了 WP 的设计准则。这个解决方案虽然有效!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2012-12-10
    • 2012-11-09
    • 1970-01-01
    • 2015-09-08
    相关资源
    最近更新 更多