xaml布局:滚动条在程序执行期间消失

时间:2016-10-03 20:29:56

标签: wpf xaml

我为我正在研究的辅助项目编写了一个简单的xaml布局。我的TextBox的滚动条将在我第一次启动应用程序时显示,但是一旦我开始将输出打印到文本框,它就会消失,我不知道为什么。其他答案说我需要在文本框上设置静态高度并使用VerticalScrollBarVisibility,所以我已经这样做但问题仍然存在。请参阅下面的屏幕截图,感谢所有帮助。

第一次启动时应用程序的图像1: enter image description here 输出开始打印时应用程序的图像2和图像2:

enter image description here

<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 个答案:

答案 0 :(得分:0)

文本框扩展超出窗口大小的主要原因是由于此行

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