макет xaml: полоса прокрутки исчезает во время выполнения программы

#wpf #xaml

#wpf #xaml

Вопрос:

Я написал простой макет xaml для побочного проекта, над которым я работаю. Полоса прокрутки для моего текстового поля будет отображаться в начале, когда я впервые запускаю свое приложение, однако, как только я начну печатать выходные данные в текстовом поле, она исчезнет, и я не уверен, почему. В других ответах говорится, что мне нужно установить статическую высоту в моем текстовом поле и использовать VerticalScrollBarVisibility, поэтому я это сделал, но проблема сохраняется. Смотрите скриншоты ниже, любая помощь приветствуется.

изображение 1 приложения при первом запуске: введите описание изображения здесьи изображение 2 приложения при запуске вывода на печать:

введите описание изображения здесь

 <Window x:Class="DashboardDeployer.MainWindow"
        x:Name="MainWindowName"
        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:Utility="clr-namespace:DashboardDeployer.Utility" mc:Ignorable="d" Title="Dashboard Deployer"
        DataContext="{Binding Main, Source={StaticResource Locator}}" Height="550" Width="1200" Margin="10" Icon="/DashboardDeployer;component/Blue-Monster.ico">

    <Window.Resources>
    <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
  <StackPanel x:Name="TitlePanel" MaxWidth="750"
            Margin="10" >
    <TextBlock Text="{Binding WelcomeTitle}"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               FontSize="25" />
    <Grid x:Name="ContentGrid" HorizontalAlignment="Left" VerticalAlignment="Stretch"
          FlowDirection="LeftToRight" MaxWidth="750" >
      <Grid.RowDefinitions>
        <RowDefinition Height="66*" />
        <RowDefinition Height="66*" />
        <RowDefinition Height="29*" />
        <RowDefinition Height="70*" />
        <RowDefinition Height="29" />
        <RowDefinition Height="Auto" />
        <RowDefinition x:Name="LastRow" Height="Auto" />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" MaxWidth="750"/>
      </Grid.ColumnDefinitions>

      <Grid x:Name="BaseDirGrid" HorizontalAlignment="Stretch" FlowDirection="LeftToRight" Grid.Row="0" >
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition Width="110"/>
          <ColumnDefinition Width="110"/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">Dex Dashboard base directory</Label>
        <TextBox Height="23" Grid.Row="1" MinWidth="300" HorizontalAlignment="Stretch"  Margin="5" Grid.Column="0" 
               Background="{Binding LocationPath, Converter={StaticResource ValidHtmlPathBrushConverter}, UpdateSourceTrigger=Explicit}"  
               Text="{Binding LocationPath, Mode=TwoWay}" KeyUp="PathTextBox_KeyUp"
               />
        <Button  Grid.Row="1" Grid.Column="1" Margin="5" Height="30" Width="100" 
              Command="{Binding BrowseSourceCommand, Mode=OneWay}" >Browse</Button>
        <Button  Grid.Row="1" Grid.Column="2" Height="30" Margin="5" Width="100" 
              Command="{Binding OpenSourceCommand, Mode=OneWay}" 
              IsEnabled="{Binding LocationPath, Converter={StaticResource ValidPathBoolConverter}}" >Explorer</Button>
      </Grid>

      <Grid x:Name="OutputDirGrid"  HorizontalAlignment="Stretch" FlowDirection="LeftToRight" Grid.Row="1" >
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto"  />
          <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"  />
          <ColumnDefinition Width="110"/>
          <ColumnDefinition Width="110*"/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">Output Directory</Label>
        <TextBox Height="23"  Grid.Row="1" MinWidth="300" HorizontalAlignment="Stretch"  Margin="5" Grid.Column="0" 
               Background="{Binding OutputPath, Converter={StaticResource ValidPathConverter}, UpdateSourceTrigger=Explicit}"  
               Text="{Binding OutputPath, Mode=TwoWay}" KeyUp="PathTextBox_KeyUp"
               />
        <Button  Grid.Row="1" Grid.Column="1" Margin="5" Height="30" Width="100" 
              Command="{Binding BrowseDestCommand, Mode=OneWay}" >Browse</Button>
        <Button Grid.Row="1" Grid.Column="2" Height="30" Margin="5" Width="100" 
              Command="{Binding OpenDestCommand, Mode=OneWay}" 
              IsEnabled="{Binding OutputPath, Converter={StaticResource ValidPathBoolConverter}}" CommandParameter="{Binding OutputPath}">Explorer</Button>
      </Grid>
      <StackPanel Grid.Row="2">
      <DockPanel  FlowDirection="LeftToRight">

        <Label>Change the populate document parameter: </Label>
          <CheckBox x:Name="ChangePopulateDocument" IsChecked="{Binding Path=ChangePopulateDocument}" HorizontalAlignment="Left" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center" />
      </DockPanel>
        <DockPanel Visibility="{Binding ChangePopulateDocument, Converter={StaticResource BooleanToVisibilityConverter} }" FlowDirection="LeftToRight">
          <Label>Check to add (false), uncheck to remove parameter</Label>
          <CheckBox x:Name="PopulateDocumentRequiresParameter" IsChecked="{Binding Path=PopulateDocumentRequiresParameter}" HorizontalAlignment="Left" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center" />
        </DockPanel>
      </StackPanel>
      <Button x:Name="Deploy" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" Height="50" Margin="10" Width="150" 
              Command="{Binding PackageAndDeployCommand, Mode=OneWay}" 
              IsEnabled="{Binding SourceAndDestAreValid}" >Deploy</Button>
      <Label Grid.Row="4" >Have you gotten latest and compiled in release mode?</Label>
      <ProgressBar x:Name="TheProgressBar" Grid.Row="5" Height="20" Margin="20 5" IsIndeterminate="{Binding IsProcessing}"  />
      <TextBox  Name="OutputLog" Grid.Row="6" Grid.ColumnSpan="3" VerticalAlignment="Stretch"  Text="{Binding ConsoleText}"
                VerticalScrollBarVisibility="Visible" AcceptsReturn="True" TextWrapping="Wrap" 
                Height="150" />
    </Grid>

  </StackPanel>


</Window>
  

Комментарии:

1. Если вы не обрезали нижнее изображение самостоятельно, текстовое поле, скорее всего, выходит за пределы окна. Обратите внимание, что отсутствует правая боковая граница. Пожалуйста, опубликуйте свой код XAML, тогда мы, вероятно, сможем увидеть, что происходит.

Ответ №1:

Основная причина, по которой текстовое поле расширяется за пределы размера окна, заключается в этой строке

 <ColumnDefinition Width="Auto" MaxWidth="750"/>
  

Как правило, макет излишне сложен. Ошибусь ли я, если предположу, что это было сделано путем перетаскивания в конструкторе? Наличие определений столбцов как 66 * 29 * и т. Д. Очень затрудняет управление макетом и оценку того, как он будет выглядеть во время выполнения.

Вот как я бы это структурировал. Он был разработан в Kaxaml, поэтому мне пришлось удалить конвертеры и обработчики событий.

 <Page
        x:Name="MainWindowName"
        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:Utility="clr-namespace:DashboardDeployer.Utility" mc:Ignorable="d" Title="Dashboard Deployer"
        Margin="10">

    <Page.Resources>
    </Page.Resources>
  <Grid x:Name="TitlePanel" Margin="10" HorizontalAlignment="Stretch">
    <TextBlock Text="{Binding WelcomeTitle}"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               FontSize="25" />
    <Grid x:Name="ContentGrid" VerticalAlignment="Stretch" FlowDirection="LeftToRight" >
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
      </Grid.RowDefinitions>

      <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*"/>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0">Dex Dashboard base directory</Label>
        <TextBox Height="23" Grid.Row="1" HorizontalAlignment="Stretch" Margin="5" Grid.Column="0" 
               Background="Green" Text="{Binding LocationPath, Mode=TwoWay}"/>
        <Button  Grid.Row="1" Grid.Column="1" Margin="5" Height="30" Width="100" 
              Command="{Binding BrowseSourceCommand, Mode=OneWay}" >Browse</Button>
        <Button  Grid.Row="1" Grid.Column="2" Height="30" Margin="5" Width="100" 
              Command="{Binding OpenSourceCommand, Mode=OneWay}" 
              IsEnabled="True" >Explorer</Button>

        <Label Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3">Output Directory</Label>
        <TextBox Height="23" Grid.Row="3" HorizontalAlignment="Stretch" Margin="5" Grid.Column="0" 
               Background="Red"  
               Text="{Binding OutputPath, Mode=TwoWay}"/>
        <Button Grid.Row="3" Grid.Column="1" Margin="5" Height="30" Width="100"
              Command="{Binding BrowseDestCommand, Mode=OneWay}" >Browse</Button>
        <Button Grid.Row="3" Grid.Column="2" Height="30" Margin="5" Width="100" 
              Command="{Binding OpenDestCommand, Mode=OneWay}" 
              IsEnabled="True">Explorer</Button>
              </Grid>

      <StackPanel Grid.Row="1">
        <DockPanel  FlowDirection="LeftToRight">  
            <Label>Change the populate document parameter: </Label>
            <CheckBox x:Name="ChangePopulateDocument" IsChecked="{Binding Path=ChangePopulateDocument}" HorizontalAlignment="Left" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center" />
        </DockPanel>
        <DockPanel Visibility="Visible" FlowDirection="LeftToRight">
            <Label>Check to add (false), uncheck to remove parameter</Label>
            <CheckBox x:Name="PopulateDocumentRequiresParameter" IsChecked="{Binding Path=PopulateDocumentRequiresParameter}" HorizontalAlignment="Left" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center" />
          </DockPanel>
      </StackPanel>

      <Grid Grid.Row="2">
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
          <RowDefinition Height="*" />
        </Grid.RowDefinitions>
      <Button x:Name="Deploy" Grid.Row="0" Grid.Column="0" Height="50" Margin="10" Width="150" 
              Command="{Binding PackageAndDeployCommand, Mode=OneWay}" 
              IsEnabled="{Binding SourceAndDestAreValid}" >Deploy</Button>
      <Label Grid.Row="1" >Have you gotten latest and compiled in release mode?</Label>
      <ProgressBar x:Name="TheProgressBar" Grid.Row="2" Height="20" Margin="20,5" IsIndeterminate="{Binding IsProcessing}"  />
      <TextBox  Name="OutputLog" Grid.Row="3" VerticalAlignment="Stretch" 
                VerticalScrollBarVisibility="Visible" AcceptsReturn="True" TextWrapping="Wrap">
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. 
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. 
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. 
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. 
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. 
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. 
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. 
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. 
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. 
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. 
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
                The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.
      </TextBox>
      </Grid>
    </Grid>
  </Grid>
</Page>