2010-09-13 20 views
1

我對Silverlight相當陌生。我正在開發Windows Phone平臺。 我要放置在其將來自該web服務綁定到數據列表框條目的端部的按鈕(我使用的是列表框模板)Silverlight/WP7:我想在按照數據綁定的列表框元素後放置一個按鈕

  • 列表項1名
  • 列表項2
  • 列表項目3
  • 列表項4
  • 列表項5
  • 列表項6
  • 列表項7
  • 列表項8
  • ..Button ..

我嘗試使用網格/ StackPanel的等主辦的按鈕和我所有的解決方案放置該按鈕在我的屏幕的底部,而不是底部的事數所有可能跨越多個屏幕的列表框條目。

我有以下XAML文件。我想添加下面的「LBoxItems」按鈕

<Grid x:Name="LayoutRoot" 
     Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <Grid x:Name="ads" > 
     <!--TitlePanel contains the name of the application and page title--> 
    <StackPanel Margin="24,24,0,12" 
       x:Name="SearchTitle"> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock FontWeight="Bold" 
         FontSize="{StaticResource PhoneFontSizeLarge}" 
         Text="{Binding Location}" /> 
      <TextBlock FontSize="{StaticResource PhoneFontSizeLarge}" 
         Text=" > " /> 
      <TextBlock FontWeight="Bold" 
         FontSize="{StaticResource PhoneFontSizeLarge}" 
         Text="{Binding Category}" /> 
     </StackPanel> 
     <TextBlock FontSize="{StaticResource PhoneFontSizeMedium}" 
        Text="{Binding Converter={StaticResource SearchHistoryItemSubTitleConverter}}" /> 
     </StackPanel> 

    </Grid> 
    <!--ContentPanel - place additional content here--> 

    <Grid x:Name="ContentGrid" 
      Grid.Row="2">    
      <ListBox x:Name="LBoxItems" 
       HorizontalAlignment="Left" 
       Margin="24, 0" 
       SelectionChanged="LBoxItems_SelectionChanged"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 

         <StackPanel Margin="{StaticResource PhoneTouchTargetOverhang}" > 
          <TextBlock FontSize="{StaticResource PhoneFontSizeMediumLarge}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Foreground="{StaticResource PhoneAccentBrush}"           
            Text="{Binding Title.Text}" TextWrapping="Wrap" Margin="-4,20,0,0"> 
          </TextBlock> 
          <TextBlock Text="{Binding PublishDate, Converter={StaticResource ItemPublishDateConverter}}" /> 

         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox>    
    </Grid> 

    <l:SpinnerControl x:Name="SpinnerControl" 
         Width="55" 
         Height="55" 
         Grid.RowSpan="2" /> 

    <TextBlock x:Name="TxtNoResultsMessage" 
       FontSize="{StaticResource PhoneFontSizeLarge}" 
       Text="No results found" 
       VerticalAlignment="Center" 
       HorizontalAlignment="Center" 
       Grid.RowSpan="2" 
       Visibility="Collapsed" /> 
</Grid> 

回答

1

你可以使用一個ScrollViewer

<ScrollViewer> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="800"></RowDefinition> 
       <RowDefinition Height="Auto"></RowDefinition> 
      </Grid.RowDefinitions> 

      <ListBox Grid.Row="0"> 

      </ListBox> 

      <Button Grid.Row="1" Height="30" Content="Test"></Button> 
     </Grid> 
</ScrollViewer> 

簡單地劃分電網內分成兩行,第二個是爲按鈕

針對您的特殊情況下,這將是:

<Grid x:Name="ContentGrid" 
      Grid.Row="2">  
     <ScrollViewer> 
      <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="600"></RowDefinition> 
       <RowDefinition Height="Auto"></RowDefinition> 
      </Grid.RowDefinitions> 

      <ListBox x:Name="LBoxItems" 
       HorizontalAlignment="Left" 
       Margin="24, 0" 
       SelectionChanged="LBoxItems_SelectionChanged" Grid.Row="0"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 

         <StackPanel Margin="{StaticResource PhoneTouchTargetOverhang}" > 
          <TextBlock FontSize="{StaticResource PhoneFontSizeMediumLarge}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Foreground="{StaticResource PhoneAccentBrush}"           
            Text="{Binding Title.Text}" TextWrapping="Wrap" Margin="-4,20,0,0"> 
          </TextBlock> 
          <TextBlock Text="{Binding PublishDate, Converter={StaticResource ItemPublishDateConverter}}" /> 

         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
      <Button Content="Sample" Height="30" Grid.Row="1" /> 
      </Grid>  
     </ScrollViewer>  
    </Grid> 

當然,你可以設置根據自己的情況適當的高度。

+0

感謝Dennis的回覆。如果我嘗試添加滾動查看器,那麼我的列表框項目將停止滾動,即我無法滾動超出屏幕上可以看到的內容。我編輯了我的原始文章以包含我正在使用的xaml。你可以從XAML建議在哪裏添加滾動查看器?再次感謝你的回覆。 – gforg 2010-09-13 06:28:39

+0

檢查編輯的答案。 – 2010-09-13 06:34:23

+0

再次感謝丹尼斯的快速回復。然而,如果我不知道高度,即由於它是數據綁定的,它可以從一個條目到100個條目變化。它看起來像按鈕將始終顯示在指定的高度之下。所以它不會立即出現在列表框之後。 – gforg 2010-09-13 08:39:17

1

你想要做的,實際上是混合和匹配DataTemplate中的不同項目。如果您對其他解決方案的效果不太滿意,我可能會考慮將按鈕添加到數據模板,但將其可見性設置爲摺疊。然後,對於最後一個條目,將其可見性設置爲可見。

無論哪種方式,你有一點點的黑客,但這種方式的按鈕是在你的列表框中。如果所有按鈕事件都指向同一個處理程序,並且只有一個按鈕事件可見,那麼應該很好。

+0

我更喜歡這種方法,因爲它更簡單,並保持列表框中的按鈕。謝謝。 – JoshSchlesinger 2011-04-09 07:03:38

0

我有同樣的問題,並嘗試使用ScrollViewer方法,但是我無法擺脫'卡住的滾動'問題,直到我禁用列表框上的垂直滾動條。也許這也會幫助其他人。

+0

謝謝,一直試圖找出爲什麼發生。 – Mark 2011-01-01 13:26:54

相關問題