Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I've created a window with a large, multi-line textbox, with rows of buttons above
and below it.

What I'm trying to do is to introduce scrollbars on the textbox when the window is too narrow
for the text within the textbox, then introduce scrollbars for the grid control itself when
the window is narrowed to the point that it is too narrow to show the panel that the buttons
are within.

The vertical aspect seems to work well enough. It's the horizontal aspect that I'm having
difficulty with.

I've commented out the things that I've tried so far.

What I have now works the best from what I've tried.

Any assistance would be most appreciated.

What I have tried:

XML
<Window x:Class="Wpf_size_01.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Wpf_size_01"
        mc:Ignorable="d"  Top="24" Left="48"
        xmlns:tools="clr-namespace:MyApp.Tools"
        Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={tools:RatioConverter}, ConverterParameter='0.9' }" 
        Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={tools:RatioConverter}, ConverterParameter='0.9' }" 
        Title="MainWindow">

    <!--  This one gets scroll on the grid.  -->
    <!--<ScrollViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretch" removed="DarkRed"
                            HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
                            Width="Auto" Height="Auto" Margin="4,4,8,48" >
        <Grid>-->

    <!--  This one gets scroll on the textbox. (This is better, but not quite there.) -->
    <ScrollViewer removed="DarkRed"
                  Width="Auto" Height="Auto" Margin="4,4,8,48" >
        <Grid ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
                <Grid.RowDefinitions>
            <RowDefinition  Height="*"/>
            <RowDefinition Height="680"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
                <!--<ColumnDefinition Width="1680"></ColumnDefinition>-->
                <ColumnDefinition Width="1*"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <!--<StackPanel HorizontalAlignment="Stretch" Height="Auto" Margin="24,48,88,148" VerticalAlignment="Stretch" Width="Auto" Grid.Row="0" Orientation="Vertical">-->
            <StackPanel FlowDirection="LeftToRight" Orientation="Horizontal" Grid.Row="0" Grid.Column="0" Height="44"
                        HorizontalAlignment="Center" removed="Aquamarine" VerticalAlignment="Center" Width="832" Margin="24,8,38,28">
                <Button x:Name="button1" Width="88" Height="34" Content="Button a"  Margin="212,4,64,4" HorizontalAlignment="Center"/>
                <Button x:Name="button2" Width="88" Height="34" Content="Button b"  Margin="4,4,64,4" HorizontalAlignment="Center"/>
                <Button x:Name="button3" Width="88" Height="34" Content="Button c"  Margin="4,4,4,4"  HorizontalAlignment="Center" />
            </StackPanel>
            <!--<DockPanel HorizontalAlignment="Stretch" Height="Auto" Margin="4,8,16,20" VerticalAlignment="Stretch" removed="DarkGray" Width="Auto"
                            Grid.Row="1" Grid.Column="0">-->
            <!--<ScrollViewer HorizontalAlignment="Stretch" Height="Auto" Margin="4,8,16,20" VerticalAlignment="Stretch" removed="DarkGreen" Width="Auto"
                            HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Grid.Row="1" Grid.Column="0">-->
                <!--<TextBox x:Name="TextBox_01" Height="Auto" TextWrapping="NoWrap" Margin="4,0,4,0" ScrollViewer.HorizontalScrollBarVisibility="Auto"
                         ScrollViewer.VerticalScrollBarVisibility="Auto"  ScrollViewer.CanContentScroll="True"
                         Text="TextBox" Width="Auto" FontSize="20" FontFamily="New Courier"
                         Foreground="White" removed="DarkBlue" AcceptsReturn="True" Grid.Row="1" Grid.Column="0"/>-->
                <TextBox x:Name="TextBox_01" Height="Auto" TextWrapping="NoWrap" Margin="4,0,4,0" Focusable="True"
                         HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" TextAlignment="Left"
                         Text="TextBox with a very LLLLLLLLLLLLLLLLLLLLLLLLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONNNNNNNNNNNNNNNNNNNNNNNNNNNGGGGGGGGGGGGGGGGGGGGGG line.   "
                         Width="Auto" FontSize="20" FontFamily="New Courier" MinWidth="434"
                         Foreground="White" removed="DarkBlue" AcceptsReturn="True" Grid.Row="1" Grid.Column="0"/>
            <!--</ScrollViewer>-->
            <!--</DockPanel>-->
            <!--<DockPanel FlowDirection="LeftToRight" Grid.Row="2" Grid.Column="0" Height="44"
                HorizontalAlignment="Center" removed="Aquamarine" VerticalAlignment="Center" Width="832" Margin="24,8,38,28">-->
            <StackPanel FlowDirection="LeftToRight" Orientation="Horizontal" Grid.Row="2" Grid.Column="0" Height="44"
                        HorizontalAlignment="Center" removed="Aquamarine" VerticalAlignment="Center" Width="832" Margin="24,8,38,28">
                <Button x:Name="button4" Width="88" Height="34" Content="Button d"  Margin="212,4,64,4" HorizontalAlignment="Center"/>
                <Button x:Name="button5" Width="88" Height="34" Content="Button e"  Margin="4,4,64,4" HorizontalAlignment="Center"/>
                <Button x:Name="button6" Width="88" Height="34" Content="Button f"  Margin="4,4,4,4"  HorizontalAlignment="Center" />
            </StackPanel>
            <!--</DockPanel>-->
            <!--</StackPanel>-->
    </Grid>
    </ScrollViewer>

</Window>



C#
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Wpf_size_01 {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow:Window {
        public MainWindow() {
        InitializeComponent();
        }
    }
}

namespace MyApp.Tools
{

    [ValueConversion(typeof(string), typeof(string))]
    public class RatioConverter : MarkupExtension, IValueConverter
    {
      private static RatioConverter _instance;

      public RatioConverter() { }

      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      { // do not let the culture default to local to prevent variable outcome re decimal syntax
        double size = System.Convert.ToDouble(value) * System.Convert.ToDouble(parameter,CultureInfo.InvariantCulture);
        return size.ToString( "G0", CultureInfo.InvariantCulture );
      }

      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      { // read only converter...
        throw new NotImplementedException();
      }

      public override object ProvideValue(IServiceProvider serviceProvider)
      {
        return _instance ?? (_instance = new RatioConverter());
      }

    }
}
Posted
Comments
Foothill 14-Sep-16 16:49pm    
Is TextWrapping = "Wrap" not an option?
wylbur 14-Sep-16 16:56pm    
Hi Foothill;

I really don't want it to wrap.

I want scroll bars whenever the structure is too large for the window.

THANKS for the input!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900