将xaml添加到列表框的顶部

本文关键字:顶部 列表 xaml 添加 | 更新日期: 2023-09-27 18:02:08

我有一个列表框,其中填满了从数据合同中获取的项目。我想添加一点xaml到列表框的顶部,它从另一个数据契约中获取数据。我该怎么做呢?

<phone:PivotItem>
    <ScrollViewer>
        <StackPanel>
            <ListBox x:Name="StatusCommentsList"
                Background="Transparent"
                ItemsSource="{Binding StatusComments}"
                u:ScrollViewerMonitor.AtEndCommand="{Binding FetchMoreStatusCommentsDataCommand}" VerticalContentAlignment="Top">
                <!-- THIS DOESNT WORK-->          
                <ListBoxItem>
                    <Grid Height="auto">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="67" />
                            <ColumnDefinition Width="389"/>
                        </Grid.ColumnDefinitions>
                        <StackPanel Height="auto" Grid.Column="0" Background="Transparent">
                            <Border Background="Transparent" BorderThickness="0" Width="62" Height="62" HorizontalAlignment="Left" Margin="0,0,0,5">
                                <Image Source="{Binding Notification.context.data.created_by.image.thumbnail_link}" Width="62" Height="62"></Image>
                            </Border>
                        </StackPanel>
                        <StackPanel Height="auto" Grid.Column="1" Width="389" MaxWidth="389" Orientation="Vertical" >
                            <TextBlock TextWrapping="Wrap" Text="{Binding Notification.context.data.created_by.name}" HorizontalAlignment="Stretch" FontSize="30" VerticalAlignment="Center" Margin="0,0,0,5" Foreground="White" Width="389" MaxWidth="389" />
                            <TextBlock TextWrapping="Wrap" Text="{Binding Notification.context.data.created_on}" HorizontalAlignment="Stretch" FontSize="30" VerticalAlignment="Center" Margin="0,0,0,5" Foreground="White" Width="389" MaxWidth="389" />
                            <TextBlock TextWrapping="Wrap" Text="{Binding Notification.context.data.rich_value}" HorizontalAlignment="Stretch" FontSize="30" VerticalAlignment="Top" Margin="0,0,0,5" Foreground="White" Width="389" MaxWidth="389" />
                        </StackPanel>
                    </Grid>
                </ListBoxItem>
                <!-- /THIS DOESNT WORK -->
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel VerticalAlignment="Top" Margin="5,0,0,0">
                            <Button Style="{StaticResource JamesTransparentButton}" Padding="-5,0,-5,-5" Margin="-7,-12,-7,-7" Height="auto" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Left" UseLayoutRounding="True" FontSize="0.01">
                                <Grid Height="auto">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="67" />
                                        <ColumnDefinition Width="389"/>
                                    </Grid.ColumnDefinitions>
                                    <StackPanel Height="auto" Grid.Column="0" Background="Transparent">
                                        <Border Background="Transparent" BorderThickness="0" Width="62" Height="62" HorizontalAlignment="Left" Margin="0,0,0,5">
                                            <Image Source="{Binding created_by.image.thumbnail_link}" Width="62" Height="62"></Image>
                                        </Border>
                                    </StackPanel>
                                    <StackPanel Height="auto" Grid.Column="1" Width="389" MaxWidth="389" Orientation="Vertical" >
                                        <TextBlock Text="{Binding created_by.name}" FontSize="30" VerticalAlignment="Top" Margin="0,0,0,5" Foreground="White" />
                                        <TextBlock Text="{Binding created_on}" FontSize="30" VerticalAlignment="Top" Margin="0,0,0,5" Foreground="White" />
                                        <TextBlock TextWrapping="Wrap" Text="{Binding value}" HorizontalAlignment="Stretch" FontSize="30" VerticalAlignment="Top" Margin="0,0,0,5" Foreground="White" Width="389" MaxWidth="389" />
                                    </StackPanel>
                                </Grid>
                            </Button>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </ScrollViewer>
</phone:PivotItem>

我试着只是添加一个新的项目到列表框,但数据契约意味着我必须实例化对象的所有子级别,它会很糟糕,因为它们是不同的域。

请记住,我希望整个屏幕滚动在联合…这样它看起来就像一个长长的列表,不管第一个是默认值

将xaml添加到列表框的顶部

将第一个项目放在ListBox的外部,并禁用ListBox的ScrollViewer,这样整个东西就会一起滚动。这里有一个例子,项目是一个简单的TextBlock。你可以根据自己的需要修改。

<ScrollViewer>
       <StackPanel Orientation="Vertical">
                <TextBlock Text="Item 1"/>
                <ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding item}"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                 </ListBox>
        </StackPanel>
</ScrollViewer>