WPF嵌套项目控件

本文关键字:控件 项目 嵌套 WPF | 更新日期: 2023-09-27 18:01:04

我刚刚开始干预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>

WPF嵌套项目控件

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

系统。Windows。数据错误:40:BindingExpression路径错误:在对象"RegistryItem"(HashCode=15478206("上找不到"GuestList"属性。BindingExpression:Path=GuestList;DataItem="RegistryItem"(哈希代码=15478206(;目标元素是"ItemsControl"(名称="(;目标属性为"ItemsSource"(类型为"IEnumerable"(

因此,无法在RegistryItem对象上解析绑定到GuestList属性的数据。如果你仔细看一下类型的定义,你就会明白为什么:

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

然后一切都会正常工作。所以底线是:总是检查错误消息,尤其是绑定错误,它们通常会告诉你可能出了什么问题。由于绑定错误通常是隐藏的(因为它们不会破坏内容(,您可以使用[在另一个问题中]描述的技术(如何将绑定错误转换为运行时异常?(将它们转换为完全异常,或者至少将它们记录在其他地方。