我的应用程序中有一个RichTextBox,当我向其中添加文本时,文本框会垂直展开。如果应用程序被拉伸,我希望它动态调整大小,但如果向其添加文本,则不会扩展。
下面是我所有的xaml代码:
<Grid Background="#FF1F1F1F">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Border Grid.Row="0" BorderThickness="1" Padding="5" Margin="10,10,10,0" BorderBrush="#FF878383" CornerRadius="10">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#FF323232" Offset="1" />
</LinearGradientBrush>
</Border.Background>
<Grid UseLayoutRounding="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label Content="Server: " VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalAlignment="Left" Padding="0" Margin="3" Foreground="White" />
<Label x:Name="ServerIDLabel" Content="" VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalAlignment="Left" Padding="0" Margin="3" Foreground="White" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Port: " VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalAlignment="Left" Padding="0" Margin="3" Foreground="White" />
<Label Name="txtPort" Content="10037" VerticalAlignment="Center" Width="60" HorizontalAlignment="Center" Margin="3" Foreground="White" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="IP:" VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalAlignment="Left" Padding="0" Margin="3" Foreground="White" />
<Label Name="IPAddress" Content="255.255.255.255" VerticalAlignment="Center" Margin="3" Foreground="White" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Version:" VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalAlignment="Left" Padding="0" Margin="3" Foreground="White" />
<Label Name="lblVersionNumber" Content="3.0.0.0" VerticalAlignment="Center" Margin="3" Foreground="White" />
</StackPanel>
</StackPanel>
<Border Grid.Column="1" BorderThickness="1" Padding="5" BorderBrush="#FF878383" CornerRadius="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="ManagerNoticeLabel" Text="Notifications:" TextWrapping="Wrap" Foreground="White" />
<Button Margin="25,0" x:Name="ClearManagerNotifications" Content="Clear" Click="ClearManagerNotifications_Click" />
</StackPanel>
</Grid>
<Grid Grid.Row="1">
<DockPanel >
<RichTextBox Name="ManagerNotice" Background="#FF343434" Foreground="#FFDEDEDE" >
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>
</DockPanel>
</Grid>
</Grid>
</Border>
<StackPanel Orientation="Vertical" Grid.Column="2" >
<Button Name="btnStartServer" Content="Start" VerticalAlignment="Center" Margin="10" Click="btnStartServer_Click"></Button>
<Button Name="btnStopServer" Content="Stop" VerticalAlignment="Center" Margin="10" Click="btnStopServer_Click" IsEnabled="False" />
</StackPanel>
</Grid>
</Border>
<my:VersionDisplayControl Grid.Row="1" x:Name="versionDisplayControl" Margin="10,10,10,0" />
<my:AgentActionsControl Grid.Row="2" x:Name="agentActionsControl" Margin="10,10,10,0" />
<Border Grid.Row="3" BorderThickness="1" Padding="5" Margin="10" BorderBrush="#FF878383" CornerRadius="10">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#FF323232" Offset="1" />
</LinearGradientBrush>
</Border.Background>
<Grid>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="28"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Orientation="Horizontal" Margin="0,0,5,0" x:Name="Summary_Agents">
<Label x:Name="AgentsLabel" Content="Agents:" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label Content="0" x:Name="lblAgentCount" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label x:Name="OfLabel" Content="of" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label Content="0" x:Name="lblMaxAgentCount" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
</StackPanel>
<StackPanel Grid.Column="1" Orientation="Horizontal" Margin="5,0" x:Name="Summary_HardwareInventory" >
<Label x:Name="HdInvLabel" Content="Hard Inv" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label Content="0" x:Name="lblHdInvCount" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label x:Name="OfHdInvLabel" Content="of" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label Content="0" x:Name="lblHdInvCountMax" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
</StackPanel>
<StackPanel Grid.Column="2" Orientation="Horizontal" Margin="5,0" x:Name="Summary_SoftwareInventory" >
<Label x:Name="SfInvLabel" Content="Soft Inv" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label Content="0" x:Name="lblSfInvCount" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label x:Name="OfSfInvLabel" Content="of" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label Content="0" x:Name="lblSfInvCountMax" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
</StackPanel>
<StackPanel Grid.Column="3" Orientation="Horizontal" Margin="5,0" x:Name="Summary_E2P">
<Label x:Name="E2InvLabel" Content="E2P" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label Content="0" x:Name="lblE2InvCount" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label x:Name="OfE2InvLabel" Content="of" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
<Label Content="0" x:Name="lblE2InvCountMax" Foreground="Lime" FontWeight="Normal" FontSize="13" FontStyle="Normal" FontStretch="Normal" />
</StackPanel>
</Grid>
<!-- a grid -->
<Grid Grid.Row="1">
<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="True" >
<StackPanel Name="spAgents" />
</ScrollViewer>
</Grid>
</Grid>
</Grid>
</Border>
</Grid>
谢谢
我把它改成了DockPanel,在添加文本时,它的大小仍然会变大(见上文)
下面是另一个项目,在这个项目中,它不会随着添加的文本而增长:
<Border Grid.Column="0" Margin="5,5,0,5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DockPanel Grid.Row="0" >
<RichTextBox Name="txtMessageHistory" Margin="5" Background="#FF343434" Foreground="#FFDEDEDE" VerticalScrollBarVisibility="Visible">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>
</DockPanel>
<StackPanel Grid.Row="1" Orientation="Vertical">
<TextBox x:Name="ChatTextBox" Background="#FFDEDEDE" Foreground="Black" Margin="5,5,5,5" />
<StackPanel Orientation="Horizontal">
<Button x:Name="SendMsg" Content="Send msg " Click="btnSendMessage_Click" Margin="0,0,5,0"/>
<Button x:Name="ClearMessages" Content="Clear messages" Click="ClearMessages_Click" Margin="0,0,5,0"/>
<Button x:Name="SendMsg_Ping" Content="Send Ping" Click="SendMsg_Ping_Click" Margin="0,0,5,0" />
<Button x:Name="SendMsg_E2PRequest" Content="Send E2P Request" Click="SendMsg_E2PRequest_Click" Margin="0,0,5,0"/>
<Button x:Name="SendMsg_MARequest" Content="Send Mon App Request" Click="SendMsg_MARequest_Click" Margin="0,0,5,0"/>
<Button x:Name="SendMsg_PluginStatusRequest" Content="Send Plugin stat Request" Click="SendMsg_PluginStatusRequest_Click" Margin="0,0,5,0"/>
</StackPanel>
</StackPanel>
</Grid>
</Border>
再试一次:
移除网格和Dockpanel -尺寸仍在增长:
<Border Grid.Column="1" BorderThickness="1" Padding="5" BorderBrush="#FF878383" CornerRadius="10">
<Grid>
<RichTextBox Name="ManagerNotice" Background="#FF343434" Foreground="#FFDEDEDE" >
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>
</Grid>
</Border>
尝试(最后一次,这是有效的)
当我显式地将高度设置为某个数字时,它的行为
<DockPanel Grid.Row="1" >
<RichTextBox Height="100" Name="ManagerNotice" Margin="5" Background="#FF343434" Foreground="#FFDEDEDE" VerticalScrollBarVisibility="Visible">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>
</DockPanel>
发布于 2018-08-21 03:23:57
以下是答案,在多条评论和很好的反馈之后:
<DockPanel Grid.Row="1" >
<RichTextBox Height="100" Name="ManagerNotice" Margin="5" Background="#FF343434" Foreground="#FFDEDEDE" VerticalScrollBarVisibility="Visible">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>
</DockPanel>
https://stackoverflow.com/questions/38665470
复制相似问题