如何将ScrollViewer添加到WPF应用程序

问题描述 投票:0回答:1

我不知道WPF应用程序。当文本超出可见区域时,我被分配了一个错误来修复一个简单的问题来添加滚动条。对于第3列,滚动条不可见。另一方面,列4的滚动条。

如何在标记列中添加类似的滚动条?它不允许我发布所有代码,因为它超过了30000个字符的限制。

这是源代码。

        <Page x:Class="BinBrowser.Pages.Browser"
                      mc:Ignorable="d" 
                      d:DesignHeight="300" d:DesignWidth="300"
                      Title="Introduction">

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="300" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="400" />
                    <ColumnDefinition Width="300" />
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="60" />
                </Grid.RowDefinitions>

                <TreeView Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Source={StaticResource SortedContainerItems}}">
                    <TreeView.Resources>
                        <HierarchicalDataTemplate DataType="{x:Type m:TypeContainer}" ItemsSource="{Binding Entries}">
                        <DataTemplate DataType="{x:Type m:RealProperty}">
                            <TextBlock>
                                <TextBlock.Text>
                                    <MultiBinding StringFormat="{}{0}: {1}">
                                        <Binding Path="Name" />
                                        <Binding Path="Value" />
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>
                        </DataTemplate>
                        <DataTemplate DataType="{x:Type m:BooleanProperty}">
                            <TextBlock>
                                <TextBlock.Text>
                                    <MultiBinding StringFormat="{}{0}: {1}">
                                        <Binding Path="Name" />
                                        <Binding Path="Value" />
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>
                        </DataTemplate>
                        <DataTemplate DataType="{x:Type m:DateTimeProperty}">
                            <TextBlock>
                                <TextBlock.Text>
                                    <MultiBinding StringFormat="{}{0}: {1}">
                                        <Binding Path="Name" />
                                        <Binding Path="Value" />
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>
                        </DataTemplate>
                        <DataTemplate DataType="{x:Type m:ObjectProperty}">
                            <WrapPanel>
                                <TextBlock Text="{Binding Name}" />
                                <mui:ModernButton Margin="5,0,10,0" EllipseDiameter="16" IconWidth="10" IconHeight="10" EllipseStrokeThickness="1"
                                                          IconData="{StaticResource Rightarrow}"
                                                          Command="{Binding DataContext.NavigateCommand, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" 
                                                          CommandParameter="{Binding Value}" />
                            </WrapPanel>
                        </DataTemplate>
                    </TreeView.Resources>
                </TreeView>

                <ContentPresenter Grid.Column="2" Grid.Row="0" Content="{Binding SelectedItem}">
                    <ContentPresenter.Resources>
                        <DataTemplate DataType="{x:Type m:DateTimeProperty}" />
                        <DataTemplate DataType="{x:Type m:BooleanProperty}" />
                        <DataTemplate DataType="{x:Type m:RealProperty}" />
                        <DataTemplate DataType="{x:Type m:IntegerProperty}" />
                        <DataTemplate DataType="{x:Type m:StringProperty}" />
                        <DataTemplate DataType="{x:Type m:ArrayProperty}" />
                        <DataTemplate DataType="{x:Type m:ObjectProperty}">

                        </DataTemplate>
                        <DataTemplate DataType="{x:Type m:Entry}">
                            <StackPanel>
                                <StackPanel Orientation="Vertical">
                                    <StackPanel.Resources>
                                        <Style TargetType="StackPanel">
                                            <Setter Property="Orientation" Value="Horizontal" />
                                            <Setter Property="Margin" Value="0,0,0,4" />
                                        </Style>
                                        <Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
                                            <Setter Property="Width" Value="70" />
                                            <Setter Property="VerticalAlignment" Value="Center" />
                                        </Style>
                                        <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                                            <Setter Property="Width" Value="200" />
                                            <Setter Property="VerticalAlignment" Value="Center" />
                                        </Style>
                                    </StackPanel.Resources>
                                    <StackPanel>
                                        <Label FontWeight="Bold" Content="Creator:" />
                                        <TextBlock Text="{Binding MetaData.Creator}" />
                                    </StackPanel>
                                    <StackPanel>
                                        <Label FontWeight="Bold" Content="Created:" />
                                        <TextBlock Text="{Binding MetaData.Created}" />
                                    </StackPanel>
                                    <StackPanel>
                                        <Label FontWeight="Bold" Content="Updator:" />
                                        <TextBlock Text="{Binding MetaData.Updator}" />
                                    </StackPanel>
                                    <StackPanel>
                                        <Label FontWeight="Bold" Content="Updated:" />
                                        <TextBlock Text="{Binding MetaData.Updated}" />
                                    </StackPanel>
                                    <StackPanel>
                                        <Label FontWeight="Bold" Content="Owner:" />
                                        <TextBlock Text="{Binding MetaData.Owner.Oid}" />
                                    </StackPanel>
                                </StackPanel>
                                <ListBox Grid.Column="3" ItemsSource="{Binding Properties}">
                                    <ListBox.Resources>
                                        <DataTemplate DataType="{x:Type m:StringProperty}">
                                            <StackPanel Orientation="Vertical">
                                                <StackPanel.Resources>
                                                    <Style TargetType="StackPanel">
                                                        <Setter Property="Orientation" Value="Horizontal" />
                                                        <Setter Property="Margin" Value="0,0,0,4" />
                                                    </Style>
                                                    <Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                    <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                </StackPanel.Resources>
                                                <StackPanel>
                                                    <TextBlock FontWeight="Bold">
                                                        <TextBlock.Text>
                                                            <MultiBinding StringFormat="{}{0}:">
                                                                <Binding Path="Name" />
                                                            </MultiBinding>
                                                        </TextBlock.Text>
                                                    </TextBlock>
                                                    <Label Content="{Binding Value}" />
                                                </StackPanel>
                                            </StackPanel>
                                        </DataTemplate>
                                        <DataTemplate DataType="{x:Type m:IntegerProperty}">
                                            <StackPanel Orientation="Vertical">
                                                <StackPanel.Resources>
                                                    <Style TargetType="StackPanel">
                                                        <Setter Property="Orientation" Value="Horizontal" />
                                                        <Setter Property="Margin" Value="0,0,0,4" />
                                                    </Style>
                                                    <Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                    <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                </StackPanel.Resources>
                                                <StackPanel>
                                                    <TextBlock FontWeight="Bold">
                                                        <TextBlock.Text>
                                                            <MultiBinding StringFormat="{}{0}:">
                                                                <Binding Path="Name" />
                                                            </MultiBinding>
                                                        </TextBlock.Text>
                                                    </TextBlock>
                                                    <Label Content="{Binding Value}" />
                                                </StackPanel>
                                            </StackPanel>
                                        </DataTemplate>
                                        <DataTemplate DataType="{x:Type m:RealProperty}">
                                            <StackPanel Orientation="Vertical">
                                                <StackPanel.Resources>
                                                    <Style TargetType="StackPanel">
                                                        <Setter Property="Orientation" Value="Horizontal" />
                                                        <Setter Property="Margin" Value="0,0,0,4" />
                                                    </Style>
                                                    <Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                    <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                </StackPanel.Resources>
                                                <StackPanel>
                                                    <TextBlock FontWeight="Bold">
                                                        <TextBlock.Text>
                                                            <MultiBinding StringFormat="{}{0}:">
                                                                <Binding Path="Name" />
                                                            </MultiBinding>
                                                        </TextBlock.Text>
                                                    </TextBlock>
                                                    <Label Content="{Binding Value}" />
                                                </StackPanel>
                                            </StackPanel>
                                        </DataTemplate>
                                        <DataTemplate DataType="{x:Type m:BooleanProperty}">
                                            <StackPanel Orientation="Vertical">
                                                <StackPanel.Resources>
                                                    <Style TargetType="StackPanel">
                                                        <Setter Property="Orientation" Value="Horizontal" />
                                                        <Setter Property="Margin" Value="0,0,0,4" />
                                                    </Style>
                                                    <Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                    <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                </StackPanel.Resources>
                                                <StackPanel>
                                                    <TextBlock FontWeight="Bold">
                                                        <TextBlock.Text>
                                                            <MultiBinding StringFormat="{}{0}:">
                                                                <Binding Path="Name" />
                                                            </MultiBinding>
                                                        </TextBlock.Text>
                                                    </TextBlock>
                                                    <Label Content="{Binding Value}" />
                                                </StackPanel>
                                            </StackPanel>
                                        </DataTemplate>
                                        <DataTemplate DataType="{x:Type m:DateTimeProperty}">
                                            <StackPanel Orientation="Vertical">
                                                <StackPanel.Resources>
                                                    <Style TargetType="StackPanel">
                                                        <Setter Property="Orientation" Value="Horizontal" />
                                                        <Setter Property="Margin" Value="0,0,0,4" />
                                                    </Style>
                                                    <Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                    <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                </StackPanel.Resources>
                                                <StackPanel>
                                                    <TextBlock FontWeight="Bold">
                                                        <TextBlock.Text>
                                                            <MultiBinding StringFormat="{}{0}:">
                                                                <Binding Path="Name" />
                                                            </MultiBinding>
                                                        </TextBlock.Text>
                                                    </TextBlock>
                                                    <Label Content="{Binding Value}" />
                                                </StackPanel>
                                            </StackPanel>
                                        </DataTemplate>
                                        <DataTemplate  DataType="{x:Type m:ObjectProperty}">
                                            <StackPanel Orientation="Vertical">
                                                <StackPanel.Resources>
                                                    <Style TargetType="StackPanel">
                                                        <Setter Property="Orientation" Value="Horizontal" />
                                                        <Setter Property="Margin" Value="0,0,0,4" />
                                                    </Style>
                                                    <Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                    <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                                                        <Setter Property="Width" Value="150" />
                                                        <Setter Property="VerticalAlignment" Value="Center" />
                                                    </Style>
                                                </StackPanel.Resources>
                                                <StackPanel>
                                                    <Label FontWeight="Bold" Content="{Binding Name}" />
                                                    <mui:ModernButton Margin="5,0,10,0" EllipseDiameter="16" IconWidth="10" IconHeight="10" EllipseStrokeThickness="1"
                                                                  IconData="{StaticResource Rightarrow}"
                                                                  Command="{Binding DataContext.NavigateCommand, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" 
                                                                  CommandParameter="{Binding Value}" />
                                                </StackPanel>
                                            </StackPanel>
                                        </DataTemplate>
                                    </ListBox.Resources>
                                </ListBox>
                            </StackPanel>
                        </DataTemplate>
                    </ContentPresenter.Resources>
                </ContentPresenter>


                <ListBox Grid.Column="3" Grid.Row="0" ItemsSource="{Binding Source={StaticResource SortedHistoryItems}}">
                    <!--     <ScrollViewer Panel.ZIndex="1" Margin="-133,-130,0,0" Height="100" Width="300" Visibility="{Binding ShowSearchResults, Converter={StaticResource ResourceKey=boolToVisConverter}}">-->
                    <ListBox.ItemTemplate>
                        <DataTemplate DataType="{x:Type m:HistoryLog}">
                            <WrapPanel>
                                <TextBlock>
                                    <TextBlock.Text>
                                        <MultiBinding StringFormat="{}{0}: {1}">
                                            <Binding Path="Type" />
                                            <Binding Path="Oid" />
                                        </MultiBinding>
                                    </TextBlock.Text>
                                </TextBlock>
                                <mui:ModernButton Margin="5,0,10,0" EllipseDiameter="16" IconWidth="10" IconHeight="10" EllipseStrokeThickness="1"
                                                          IconData="{StaticResource Rightarrow}"
                                                          Command="{Binding DataContext.NavigateHistoryCommand, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" 
                                                          CommandParameter="{Binding}" />
                            </WrapPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                    <!--        </ScrollViewer> -->
                </ListBox>


                <WrapPanel Grid.Row="1" Grid.ColumnSpan="4">
                    <TextBox x:Name="searchValue" Width="100" TextChanged="searchValue_TextChanged" />
                    <mui:ModernButton Margin="5,0,10,0" EllipseDiameter="16" IconWidth="10" IconHeight="10" EllipseStrokeThickness="1"
                                                          IconData="{StaticResource Rightarrow}"
                                                          Command="{Binding SearchCommand}" 
                                                          CommandParameter="{Binding ElementName=searchValue, Path=Text}" />
                    <ScrollViewer Panel.ZIndex="1" Margin="-133,-130,0,0" Height="100" Width="300" Visibility="{Binding ShowSearchResults, Converter={StaticResource ResourceKey=boolToVisConverter}}">
                        <ListBox Grid.Column="3" Grid.Row="0" ItemsSource="{Binding SearchResult}">
                            <ListBox.ItemTemplate>
                                <DataTemplate DataType="{x:Type m:Entry}">
                                    <WrapPanel>
                                        <TextBlock>
                                            <TextBlock.Text>
                                                <MultiBinding StringFormat="{}{0}: {1}">
                                                    <Binding Path="Type" />
                                                    <Binding Path="Oid" />
                                                </MultiBinding>
                                            </TextBlock.Text>
                                        </TextBlock>
                                        <mui:ModernButton Margin="5,0,10,0" EllipseDiameter="16" IconWidth="10" IconHeight="10" EllipseStrokeThickness="1"
                                                          IconData="{StaticResource Rightarrow}"
                                                          Command="{Binding DataContext.NavigateCommand, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" 
                                                          CommandParameter="{Binding}" />
                                    </WrapPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </ScrollViewer>
                </WrapPanel>
            </Grid>
        </Page> 
wpf scrollview
1个回答
0
投票

试试这个:

如果问题出在第3栏,那可能就在我要在下面显示的内容中

<TextBlock MaxWidth="400" 
           ScrollViewer.CanContentScroll="True"
           ScrollViewer.HorizontalScrollBarVisibility="Auto" >

在这里:(<ListBox Grid.Column="3" Grid.Row="0" .....

<ListBox Grid.Column="3" Grid.Row="0" ItemsSource="{Binding Source={StaticResource SortedHistoryItems}}">
    <!--     <ScrollViewer Panel.ZIndex="1" Margin="-133,-130,0,0" Height="100" Width="300" Visibility="{Binding ShowSearchResults, Converter={StaticResource ResourceKey=boolToVisConverter}}">-->
    <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type m:HistoryLog}">
            <WrapPanel>
                <TextBlock MaxWidth="400" 
                           ScrollViewer.CanContentScroll="True"
                           ScrollViewer.HorizontalScrollBarVisibility="Auto">
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0}: {1}">
                            <Binding Path="Type" />
                            <Binding Path="Oid" />
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
                <mui:ModernButton Margin="5,0,10,0" EllipseDiameter="16" IconWidth="10" IconHeight="10" EllipseStrokeThickness="1"
                                          IconData="{StaticResource Rightarrow}"
                                          Command="{Binding DataContext.NavigateHistoryCommand, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" 
                                          CommandParameter="{Binding}" />
            </WrapPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <!--        </ScrollViewer> -->
</ListBox>

解释:对于要显示的ScrollView,必须有一个定义的宽度和高度,因为对于无限大小的对象,不可能有ScrollView

© www.soinside.com 2019 - 2024. All rights reserved.