WPF嵌套ItemsControls

时间:2015-07-05 19:38:42

标签: c# wpf itemscontrol expander

我刚开始干涉ItemsControls / Binding,我遇到了一个问题。我已经查看了有关嵌套ItemsControls的各种教程,所以我不能肯定我做错了什么。我相信所有内容都已正确编码,但Expander并没有按照应有的方式显示内容。 Header正确地将其自身对齐到其父级的顶部,但ScrollViewer不会出现,并且它仅滚动父级" TimeScrollViewer"。我可能错误地绑定了一些东西吗?

所有建议都表示赞赏。

C#:

private string[][] hours = new string[][]
{
    new string[] { "11:00", "11:30", "12:00", "12:30", "1:00", "1:30", "2:00", "2:30", "3:00", "3:30", "4:00", "4:30", "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30" },
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" },
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" },
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" },
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" },
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" },
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" }
};

public class GuestItem
{
    public string GuestName { get; set; }
}

public class RegistryItem
{
    public string Header { get; set; }
    public List<GuestItem> GuestList = new List<GuestItem>();
}

Expander currentExpander = null;

public MainWindow()
{
    int day = (int)DateTime.Now.DayOfWeek;

    InitializeComponent();

    List<RegistryItem> items = new List<RegistryItem>();

    foreach(string hour in hours[day])
    {
        RegistryItem registryItem = new RegistryItem(){ Header = hour };

        registryItem.GuestList.Add(new GuestItem() { GuestName = "Bob" });
        registryItem.GuestList.Add(new GuestItem() { GuestName = "Frank" });
        registryItem.GuestList.Add(new GuestItem() { GuestName = "Jim" });

        items.Add(registryItem);
    }

    TimeItemsControl.ItemsSource = items;
}

private void ExpanderExpanded(object sender, RoutedEventArgs e)
{
    if(currentExpander != null)
    {
        currentExpander.IsExpanded = false;
    }

    currentExpander = e.Source as Expander;

    currentExpander.IsExpanded = true;
}

private void ExpanderCollapsed(object sender, EventArgs e)
{
    currentExpander = null;
}

XAML:

<s:SurfaceScrollViewer Name="TimeScrollViewer" Grid.Row="1" Grid.Column="1" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden" Background="#4CAAAAFF" Style="{DynamicResource SurfaceScrollViewerHorizontalTop}" Foreground="#4CAAAAFF">
    <ItemsControl Name="TimeItemsControl">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Expander Expanded="ExpanderExpanded" Collapsed="ExpanderCollapsed" Header="{Binding Header}" Style="{DynamicResource SurfaceExpander}" HorizontalContentAlignment="Center" FontSize="21.333" Width="100">
                    <s:SurfaceScrollViewer Width="{Binding ElementName=TimeScrollViewer, Path=ActualWidth}" Height="{Binding ElementName=TimeScrollViewer, Path=ActualHeight}" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden">
                        <ItemsControl ItemsSource="{Binding GuestList}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <s:SurfaceButton Content="{Binding GuestName}"/>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </s:SurfaceScrollViewer>
                </Expander>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</s:SurfaceScrollViewer>

1 个答案:

答案 0 :(得分:1)

当您运行(调试)应用程序并检查Visual Studio中的输出选项卡时,您可以多次看到以下输出:

  

System.Windows.Data错误:40:BindingExpression路径错误:&#39; GuestList&#39;在&#39; object&#39;上找不到的属性&#39;&#39; RegistryItem&#39; (的HashCode = 15478206)&#39 ;. BindingExpression:路径= GuestList;的DataItem =&#39; RegistryItem&#39; (的HashCode = 15478206);目标元素是&#39; ItemsControl&#39; (名称=&#39;&#39);目标属性是&#39; ItemsSource&#39; (键入&#39; IEnumerable&#39;)

因此无法在GuestList对象上解析绑定到RegistryItem属性的数据。如果仔细查看类型的定义,可以看出原因:

public class RegistryItem
{
    public string Header { get; set; }
    public List<GuestItem> GuestList = new List<GuestItem>();
}

GuestList不是属性,而是字段。 WPF绑定引擎需要属性,因此GuestList字段尽管是公共的,但对绑定引擎不存在,导致上述错误。要解决这个问题,只需将其设为属性即可。您可以使用空构造函数初始化列表:

public class RegistryItem
{
    public string Header { get; set; }
    public List<GuestItem> GuestList { get; set; }

    public RegistryItem ()
    {
        GuestList = new List<GuestItem>();
    }
}

然后一切都会正常工作。所以底线是:始终检查错误消息,特别是绑定错误,它们通常会告诉您可能出错的地方。由于绑定错误通常是隐藏的(因为它们不会打破的东西),你可以使用[在另一个问题中]描述的技术( How can I turn binding errors into runtime exceptions?)将它们变成完全例外或者至少将它们记录在其他地方。