对于我正在开发的应用程序,我有一些嵌套的ItemsControls,在以下布局中使用了一些不相关的项目以简洁起见:
ItemsControl(GroupItemsControl) - ItemPanel:具有垂直方向的StackPanel - ItemTemplate:ScrollViewer里面有一个ItemsControl(SliceItemsControl) - ItemPanel:画布* - ItemTemplate:包含ItemsControl的Canvas(EventItemsControl) - ItemPanel:自定义面板,在测量/布局后最终将达到设定高度 - ItemTemplat:包含矩形和文本块的StackPanel,水平方向
问题在于我需要将标有“*”的画布大小调整到嵌套两层深度的自定义面板的高度(实际上它需要是最高的SliceItemsControl项的高度(自定义)它包含在其中。)目标是滚动查看器可以向左/向右滚动,但滚动查看器的高度将调整,以便不需要垂直滚动。
请注意,我正在尝试使用MVVM(我的第一次),所以我试图尽可能多地将数据绑定到ViewModel。
布局的完整xaml:
<navigation:Page.Resources>
<SPARTA_ViewModel:FlexCalendar x:Key="FlexCalendarDataSource" d:IsDataSource="True"/>
<SPARTA_HelperClasses:DateToOffsetConverter x:Key="DateOffsetConverter"/>
<SPARTA_HelperClasses:DaysToPixelsConverter x:Key="DatePixelConverter"/>
<ItemsPanelTemplate x:Key="GroupsItemPanel">
<StackPanel/>
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="SlicesPanelTemplate">
<Canvas />
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="EventsPanelTemplate">
<SPARTA_HelperClasses:FlexCalendarEventPanel x:Name="EventPanel" SizeChanged="EventPanel_SizeChanged" />
</ItemsPanelTemplate>
<DataTemplate x:Key="EventsItemTemplate">
<StackPanel Orientation="Horizontal"
>
<Rectangle Fill="Green" Width="{Binding Length}" Height="20">
<ToolTipService.ToolTip>
<TextBlock Text="{Binding StartDate}"></TextBlock>
</ToolTipService.ToolTip>
</Rectangle>
<TextBlock Text="{Binding EventName}"></TextBlock>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="SlicesItemTemplate">
<Canvas Width="{Binding Path=SliceSize, Source={StaticResource FlexCalendarDataSource}, Converter={StaticResource DatePixelConverter}, ConverterParameter={StaticResource FlexCalendarDataSource}}"
Height="300"
Background="#FF700505"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Canvas.Left="{Binding Left}"
x:Name="EventAreaCanvas">
<ToolTipService.ToolTip>
<TextBlock Text="{Binding SliceRange.StartDate}"></TextBlock>
</ToolTipService.ToolTip>
<SPARTA_HelperClasses:EventsItemsCollection x:Name="EventsItemsControl" ItemsSource="{Binding Events, Mode=OneWay}" ItemsPanel="{StaticResource EventsPanelTemplate}" ItemTemplate="{StaticResource EventsItemTemplate}"/>
</Canvas>
</DataTemplate>
<DataTemplate x:Key="GroupsItemTemplate">
<StackPanel Orientation="Horizontal" Margin="5">
<Border Width="{Binding GroupColWidth, Mode=OneWay, Source={StaticResource FlexCalendarDataSource}}" BorderBrush="Black" BorderThickness="1" MinHeight="50" CornerRadius="3" Background="#FF629433">
<TextBlock Text="{Binding Title, Mode=OneWay}" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ScrollViewer MinHeight="50"
HorizontalContentAlignment="Left"
VerticalContentAlignment="Top"
Padding="1"
VerticalScrollBarVisibility="Auto"
Width="750"
>
<SPARTA_HelperClasses:SliceItemsCollection
x:Name="SlicesItemsControl"
ItemsSource="{Binding Slices, Mode=OneWay}"
ItemsPanel="{StaticResource SlicesPanelTemplate}"
ItemTemplate="{StaticResource SlicesItemTemplate}"
Height="500"/>
</ScrollViewer>
</StackPanel>
</DataTemplate>
</navigation:Page.Resources>
<Grid x:Name="LayoutRoot" Height="768" Margin="0" Width="1024" DataContext="{Binding Source={StaticResource FlexCalendarDataSource}}" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition Height="45"/>
<RowDefinition />
<RowDefinition Height="48"/>
</Grid.RowDefinitions>
<ItemsControl x:Name="GroupsItemsControl"
Margin="0"
Grid.Row="1"
DataContext="{StaticResource FlexCalendarDataSource}"
ItemsSource="{Binding Groups, Mode=OneWay}"
ItemsPanel="{StaticResource GroupsItemPanel}"
ItemTemplate="{StaticResource GroupsItemTemplate}"
HorizontalContentAlignment="Left"
VerticalContentAlignment="Top">
</ItemsControl>
<TextBlock Margin="0" Text="{Binding Title, Mode=OneWay}" TextWrapping="Wrap" d:LayoutOverrides="Height" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" TextDecorations="Underline" FontSize="24"/>
<Button x:Name="btnLoad" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Content="Load Data"/>
<TextBlock x:Name="txtVisibleDate" HorizontalAlignment="Center" VerticalAlignment="Top" Width="75" Text="{Binding VisibleDate}"/>
</Grid>