I have an application with simple LogView
. Messages from program are collected to this log and log is scrolling so the last message is always visible to the user. In the ApplicationMainView
there is a grid with few rows and GridSplitter between two rows. The log is at the bottom of the screen and GridSplitter is above the log (user can resize the log window size).
Here comes the problem: When application starts and messages are added to the log window, GridSplitter starts moving up and the log window starts to grow. When user changes the log size with GridSplitter and messages are added to the window, GridSplitter does not move and the log window remains its size. Here is a gif image showing the situation.
Where should I change the code so that LogView
remains its size from the application start and user don’t have to move the GridSplitter?
Code for ApplicationMainView
where the LogView
is:
<Window
x:Class="UI.ApplicationMainView"
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:UI">
<Grid>
<Grid.RowDefinitions>
<RowDefinition
Height="Auto" />
<RowDefinition
Height="Auto" />
<RowDefinition
Height="*" />
<RowDefinition
Height="Auto" />
<RowDefinition
Height="Auto" />
</Grid.RowDefinitions>
<!-- Other controls removed for simplifaction-->
<local:FilesView
Grid.Row="2"
DataContext="{Binding Files}" />
<GridSplitter
Grid.Row="3"
Height="5"
ResizeDirection="Rows"
ResizeBehavior="PreviousAndNext"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
ShowsPreview="True" />
<!-- Here is the log -->
<local:LogView
Grid.Row="4"
DataContext="{Binding Log}" />
</Grid>
</Window>
Code for LogView
:
<UserControl
x:Class="UI.LogView"
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:local="clr-namespace: UI">
<HeaderedContentControl>
<HeaderedContentControl.Template>
<ControlTemplate
TargetType="HeaderedContentControl">
<DockPanel>
<ContentPresenter
DockPanel.Dock="Top"
ContentSource="Header" />
<ContentPresenter />
</DockPanel>
</ControlTemplate>
</HeaderedContentControl.Template>
<HeaderedContentControl.Header>
<Border
Background="Gray"
CornerRadius="0,20,0,0">
<Label
Content="Log" />
</Border>
</HeaderedContentControl.Header>
<Border
BorderThickness="2,0,2,2"
BorderBrush="Gray">
<Grid>
<Grid.RowDefinitions>
<RowDefinition
Height="*" />
<RowDefinition
Height="Auto" />
</Grid.RowDefinitions>
<DataGrid
x:Name="LogGrid"
Grid.Row="0"
AutoGenerateColumns="False"
CanUserAddRows="False"
IsReadOnly="True"
GridLinesVisibility="None"
MinHeight="50"
ItemsSource="{Binding AllMessages}">
<DataGrid.Columns>
<DataGridTextColumn
Binding="{Binding Time}"
CanUserSort="False"
CanUserReorder="False"
IsReadOnly="True"
Header="Time" />
<DataGridTextColumn
Binding="{Binding Message}"
Width="*"
CanUserSort="False"
CanUserReorder="False"
IsReadOnly="True"
Header="Message" />
</DataGrid.Columns>
</DataGrid>
<DockPanel
Grid.Row="1"
LastChildFill="False">
<Button
Content="Clear"
Command="{Binding ClearLogCommand}" />
<Button
Content="Copy clipboard"
Command="{Binding CopyLogClipboardCommand}" />
</DockPanel>
</Grid>
</Border>
</HeaderedContentControl>
</UserControl>
Code behind the LogView
(for scrolling the log):
using System.Collections.Specialized;
using System.Windows.Controls;
namespace UI
{
public partial class LogView : UserControl
{
public LogView()
{
InitializeComponent();
((INotifyCollectionChanged)LogGrid.Items).CollectionChanged += OnLogViewCollectionChanged;
}
private void OnLogViewCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (LogGrid.Items.Count > 0)
{
LogGrid.ScrollIntoView(LogGrid.Items[LogGrid.Items.Count - 1]);
}
}
}
}
You defined the bottom row of the grid:
This is making the LogView grow.
You must define
Height
either as a fixed sizeHeight="200"
or as a percentage of the screen
Height="*"
orHeight="2*"