我剛開始ItemsControls /綁定多管閒事,我遇到了一個問題。我已經看過有關嵌套ItemsControls的各種教程,所以我不太確定我做錯了什麼。我相信所有代碼都可以正確編碼,但Expander不會顯示它應該顯示的內容。標題正確地將其自身對齊到其父項的頂部,但ScrollViewer不會出現,並且僅滾動父母「TimeScrollViewer」。我是否可能不正確地約束某些東西?WPF嵌套ItemsControls
所有的建議表示讚賞。
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>
Yelp!這就是訣竅,非常感謝。從現在開始,我會密切關注這些錯誤! – Tannz0rz