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:
<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">
<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="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<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>
<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"/>
<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>
</Grid>
</ScrollViewer>
</Window>
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 {
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)
{
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)
{
throw new NotImplementedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return _instance ?? (_instance = new RatioConverter());
}
}
}