【问题标题】:XAML Design Data and StaticResources / CollectionViewsXAML 设计数据和静态资源/集合视图
【发布时间】:2014-03-28 10:50:16
【问题描述】:

我已经使用设计时 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 的绑定语法不正确?

【问题讨论】:

    标签: c# wpf xaml binding design-data


    【解决方案1】:

    似乎可以在 Visual Studio/Blend 2013 中使用:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多