Search code examples
wpfscrollview

How to add a ScrollViewer to the WPF application


I have no idea about WPF application. I was assigned a bug to fix a simple issue to add a scrollbar when text exceeds visible area. For column 3 scrollbar is not visible. On the other side, there is scrollbar for the column 4.

How can I add a similar scrollbar to the marked column? It doesn't allow me to post all code, because it is exceeding the limit of 30000 characters.

Here is the source code.

        <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> 

Solution

  • try this:

    if the problem is in column 3 it's probably in what I'm going to display below

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

    here: (<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>
    

    explanation: for the ScrollView to be displayed, there must be a defined width and height, because it is not possible to have a ScrollView for an object of infinite size.