0

我已经使用设计时 XAML 数据几个星期了,它似乎工作正常,除了某些情况,例如我希望列表框根据声明为资源的集合视图显示项目的当前情况。

基本上我想显示按类别分组的“事物”列表。

public class Thing
{
    public string Category { get; set; }
    public string Name { get; set; }
}

然后我通过一个虚拟类 ListOfThings 公开它

public class ListOfThings
{

    public string ListName { get; set; }
    public ObservableCollection<Thing> Things { get; set; }
}

我有一个用于数据的虚拟 XAML 文件,如下所示(XMLFile1.xaml)

<local:ListOfThings xmlns:local="clr-namespace:ListBoxGroupExample"
                    ListName="TheListOfThings">
<local:ListOfThings.Things>
    <local:Thing Name="Item1" Category="Catagory1" />
    <local:Thing Name="Item2" Category="Catagory1" />
    <local:Thing Name="Item3" Category="Catagory2" />
</local:ListOfThings.Things>

在窗口中,我有一个带有组样式的列表框,用于每个组的扩展器

<Window x:Class="ListBoxGroupExample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:ListBoxGroupExample"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    d:DataContext="{d:DesignData Source=/DesignData/XMLFile1.xaml}"
    mc:Ignorable="d">
<Window.Resources>

    <CollectionViewSource x:Key="GroupedData" Source="{Binding Path=Things}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Category" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

</Window.Resources>
<Grid>
    <StackPanel>

        <ListBox ItemsSource="{Binding Source={StaticResource GroupedData}}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}" />
                </DataTemplate>
            </ListBox.ItemTemplate>

            <ListBox.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander IsExpanded="True">
                                            <Expander.Header>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
                                            </Expander.Header>
                                            <ItemsPresenter Margin="5,0,0,0" />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListBox.GroupStyle>

        </ListBox>
    </StackPanel>
</Grid>

这在运行时有效(我创建了一个 ListOfThings 实例,填充 Observable 集合以具有与 XAML 设计数据相同的结构)但在设计时我什么也得不到。

如果我将 ItemSource 更改为直接绑定到事物的 ObservableCollection 而不是集合视图,它可以工作,但我当然没有分组。

ItemsSource="{Binding Path=Things}"

我是否误解了静态资源的某些内容,因为它们在设计时不可用?因此,为什么绑定到 collectionview 不呈现?

还是对 Source 的绑定语法不正确?

4

1 回答 1

0

似乎在 Visual Studio/Blend 2013 中工作:

在此处输入图像描述

于 2014-02-26T02:48:11.867 回答