如何使用ScrollViewer绑定控件?

时间:2014-08-29 08:59:40

标签: xaml windows-phone-8

以下代码我有。我正在使用Listbox使用绑定。在列表框下方,我有一个提交按钮。能够滚动Listbox项目只能滚动按钮。按钮位于屏幕底部(不是列表)。我希望这个按钮位于列表框的底部?

 <ScrollViewer VerticalScrollBarVisibility="Visible" Height="780" MaxHeight="1800" VerticalAlignment="Top">
     <ScrollViewer.Content> 
         <Grid Grid.Row="0" >
             <Grid.RowDefinitions>
                 <RowDefinition Height="Auto"/>
             </Grid.RowDefinitions>
             <ListBox Name="formDetails" ItemsSource="{Binding}" Grid.Row="0" Height="780" MaxHeight="1800">
                 <ListBox.ItemTemplate>
                     <DataTemplate>
                         <StackPanel Name="Text">
                             <TextBlock Name="Txt_Question" 
                                 Text="{Binding Question, Mode=OneWay}"/>
                             <TextBox Name="TxTAnswer" 
                                 Text="{Binding Stringval, Mode=TwoWay}" 
                                 Visibility="{Binding DataType, Mode=OneWay, Converter={StaticResource TextConverter}}"/>
                             <CheckBox Name="BoolVal" 
                                 IsChecked="{Binding BoolVal, Mode=TwoWay}" 
                                 Visibility="{Binding DataType, Mode=OneWay, Converter={StaticResource YesNoConverter}}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button  Grid.Row="1" Click="Button_Click" Content="Submit"/>
        </Grid>
    </ScrollViewer.Content>
</ScrollViewer>

2 个答案:

答案 0 :(得分:1)

尝试使用列表框底部的按钮:

    <ScrollViewer VerticalScrollBarVisibility="Visible" VerticalAlignment="Top">
        <StackPanel>
            <ListBox Name="formDetails" ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Name="Text">
                            <TextBlock Name="Txt_Question" 
                         Text="{Binding Question, Mode=OneWay}"/>
                            <TextBox Name="TxTAnswer" 
                         Text="{Binding Stringval, Mode=TwoWay}" 
                         Visibility="{Binding DataType, Mode=OneWay, Converter={StaticResource TextConverter}}"/>
                            <CheckBox Name="BoolVal" 
                         IsChecked="{Binding BoolVal, Mode=TwoWay}" 
                         Visibility="{Binding DataType, Mode=OneWay, Converter={StaticResource YesNoConverter}}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button  Click="Button_Click" Content="Submit"></Button>
        </StackPanel>
    </ScrollViewer>

页面底部的按钮

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="auto"></RowDefinition>
    </Grid.RowDefinitions>
    <ScrollViewer VerticalScrollBarVisibility="Visible" VerticalAlignment="Top">
        <StackPanel>
            <ListBox Name="formDetails" ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Name="Text">
                            <TextBlock Name="Txt_Question" Text="{Binding Q}"></TextBlock>
                            <TextBox Name="TxTAnswer" Text="{Binding A}"></TextBox>
                            <CheckBox Name="BoolVal" IsChecked="{Binding Val}"></CheckBox>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </ScrollViewer>
    <Button  Click="Button_Click" Content="Submit" Grid.Row="1"></Button>
</Grid>

答案 1 :(得分:0)

一些事情:

  1. 您已将按钮定义为Grid.Row="1",但不存在。您需要添加第二行,否则它将与ListBox显示在同一行中。
  2. 您的整个页面都在ScrollViewer之内,这真的是您想要的吗?如果你想在滚动查看器之外使用Button,那么只需将ListBox包裹在ScrollViewer中,但这有点毫无意义,因为ListBox内置了ScrollViewer