1

ボタンを含むスタックパネルでスクロールバーなしでWPF ScrollViewerを使用しようとしています。

    <ScrollViewer Grid.Row="1" Name="scrollViewer1" VerticalScrollBarVisibility="Hidden">
        <StackPanel Name="stackPanel1">
            <Button Content="Button1" Height="23" Name="button1" MinHeight="75" />
            <Button Content="Button2" Height="23" Name="button2" MinHeight="75" />
            <Button Content="Button3" Height="23" Name="button3" MinHeight="75" />
            <Button Content="Button4" Height="23" Name="button4" MinHeight="75" />
            <Button Content="Button5" Height="23" Name="button5" MinHeight="75" />
        </StackPanel>
    </ScrollViewer>

ウィンドウの別の場所で「スクロールアップ」および「スクロールダウン」ボタンを使用したい (これは、車内の小さな画面で使用される可能性が高い)。scrollViewer1.LineDown() などを使用して簡単に実行できますが、要素がクリップされているかビューポートの外側にある場合にのみ、「スクロールアップ/スクロールダウン」ボタンを表示したいと思います。

ここから始める方法がわかりません。各要素をテストする必要がありますか?

どんなポインタでも大歓迎です!

よろしく、ジェイソン

4

1 に答える 1

0

このコードは、上下ボタンを有効/無効にします。

XAML:

<Window x:Class="ScrollTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ScrollViewer Grid.Row="0" ScrollChanged="OnScrollChanged">
            <StackPanel>
                <Button Content="Button1" Height="50" />
                <Button Content="Button2" Height="50" />
                <Button Content="Button3" Height="50" />
                <Button Content="Button4" Height="50" />
                <Button Content="Button5" Height="50" />
            </StackPanel>
        </ScrollViewer>
        <Button Grid.Row="1" Name="_upButton" Content="Up" />
        <Button Grid.Row="2" Name="_downButton" Content="Down" />
    </Grid>
</Window>

コードビハインド:

private void OnScrollChanged(object sender, ScrollChangedEventArgs e)
{
   _upButton.IsEnabled = (sender as ScrollViewer).VerticalOffset > 0;
   _downButton.IsEnabled = (sender as ScrollViewer).VerticalOffset < (sender as ScrollViewer).ScrollableHeight;
}
于 2010-06-07T21:20:29.833 に答える