C# WPF ScrollViewer в UserControl не работает - PullRequest
0 голосов
/ 09 февраля 2020

Мне нужна помощь с моим ScollerViewer. Вот мой код:

<UserControl x:Class="ConfigUI.Views.ProfileListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:local="clr-namespace:ConfigUI.Views"
             mc:Ignorable="d" 
             HorizontalAlignment="Center" VerticalAlignment="Top" 
             d:DesignHeight="700" d:DesignWidth="960">
    <Grid Margin="20" Height="500"
          ScrollViewer.CanContentScroll="True"
          ScrollViewer.VerticalScrollBarVisibility="Visible">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

       <Border ...
       /Border>

       <Border ...  /Border>

       <Border ... /Border>

   </Grid>
</UserControl>

Я хочу показать полосу прокрутки, когда этот элемент управления показывает, и пользователи могут прокручивать, чтобы просмотреть содержимое.

Но теперь полоса прокрутки просто не будет отображаться ...

Кто-нибудь может помочь? Спасибо.

1 Ответ

1 голос
/ 09 февраля 2020

Вы не можете иметь ScrollViewer на Grid напрямую

Попробуйте эту структуру


<ScrollViewer VerticalScrollBarVisibility="Visible" CanContentScroll="True">
    <Grid Margin="20" Height="Auto">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

       <Border ...
       /Border>

       <Border ...  /Border>

       <Border ... /Border>

   </Grid>
</ScrollViewer>


...