无论滚动如何,如何保持ScrollViewer中的元素

时间:2012-06-24 14:27:03

标签: silverlight xaml scrollviewer

在Silverlight中,是否可以让ScrollViewer中的某些元素留在视图中?我的意思是,无论当前滚动位置在哪里,某些元素都应始终可见。

以XAML为例(你可以把它放在类似XamlPad的KaXaml中):

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>  
    <ScrollViewer Width="250" Height="100" HorizontalScrollBarVisibility="Visible">
      <StackPanel>
        <Grid Width="350" Height="50">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
          </Grid.ColumnDefinitions>
          <Button Content="1" Grid.Column="0" BorderThickness="2" Width="50" />
          <Button Content="2" Grid.Column="1" BorderThickness="2" Width="50" />
          <Button Content="3" Grid.Column="2" BorderThickness="2" Width="50" />
          <Button Content="4" Grid.Column="3" BorderThickness="2" Width="50" />
          <Button Content="5" Grid.Column="4" BorderThickness="2" Width="50" />
          <Button Content="6" Grid.Column="5" BorderThickness="2" Width="50" />
          <Button Content="X" Grid.Column="6" BorderThickness="2" Width="50" />
        </Grid>
        <Grid Width="350" Height="50">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="50" />
          </Grid.ColumnDefinitions>
          <Button Content="1" Grid.Column="0" BorderThickness="2" Width="50" />
          <Button Content="2" Grid.Column="1" BorderThickness="2" Width="50" />
          <Button Content="3" Grid.Column="2" BorderThickness="2" Width="50" />
          <Button Content="4" Grid.Column="3" BorderThickness="2" Width="50" />
          <Button Content="5" Grid.Column="4" BorderThickness="2" Width="50" />
          <Button Content="6" Grid.Column="5" BorderThickness="2" Width="50" />
          <Button Content="X" Grid.Column="6" BorderThickness="2" Width="50" />
        </Grid>
      </StackPanel>
    </ScrollViewer>
  </Grid>
</Page>

无论您向左或向右滚动多少,我都希望看到“X”按钮。当然,向下或向上滚动时,“X”按钮应该跟随滚动。

我不是说我的XAML的结构是理想的,但是每一行都是对不同项的数据绑定,因此每一行都是一个数据模板。我不能在滚动查看器中放置一个大网格,或者将列而不是行作为模板(至少,我认为我不能)。

2 个答案:

答案 0 :(得分:1)

雅没有汗,只需在这样的面板中将这些元素叠加在Scrollviewer上;

<Grid>

  <ScrollViewer>
     ... Scroll Viewer Embedded Stuff ...
  </ScrollViewer>
  <StackPanel Orientation="Horizontal" 
              HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5">
     <Button Content="Button1"/>
     <Button Content="Button1"/>
     <Button Content="Button1"/>
     <Button Content="Button1"/>
     <Button Content="Button1"/>
  </StackPanel>

</Grid>

你甚至可以更进一步,设置一个EventTrigger,使你的按钮在MouseEnter上可见,如果你愿意,可以在MouseLeave上消失,但这应该让你开始。祝你好运!

答案 1 :(得分:0)

使用DataGrid可能会更幸运,并将X按钮作为冻结列。