问题描述
我有一个Xaml视图,试图在Exander内部的网格内显示文本框。
<Expander DataContext="{Binding DiagnosticCategories[0].DiagnosticResults[0]}" <!-- For the test -->
Background="Transparent"
Foreground="{StaticResource ActiveForegroundBrush}"
IsExpanded="False">
<Grid Margin="10">
<Grid.ColumnDeFinitions>
<ColumnDeFinition Width="*" SharedSizeGroup="A" />
<ColumnDeFinition Width="*" SharedSizeGroup="A" />
<ColumnDeFinition Width="auto" />
</Grid.ColumnDeFinitions>
<Grid.RowDeFinitions>
<RowDeFinition Height="auto" />
<RowDeFinition Height="auto" />
<RowDeFinition Height="auto" />
</Grid.RowDeFinitions>
<StackPanel
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Top">
<TextBox
Margin="10"
Background="Transparent"
BorderThickness="0"
FontSize="13"
FontWeight="Light"
Foreground="{StaticResource ActiveForegroundBrush}"
IsReadOnly="True"
Opacity="0.8"
ScrollViewer.HorizontalScrollBarVisibility="disabled"
SelectionBrush="Black"
Text="{Binding FormatedParameters,Mode=OneWay}"
textwrapping="Wrap" />
</StackPanel>
[...]
</Grid>
</Expander>
但是,即使我的文本只是“ aa”,文本框也有一个正常高度的问题...
首先,我认为问题出在Grid.Row,而文本框只适合它,因此我尝试添加一个不适合Grid.Row的StackPanel,但它不起作用。看来问题出在文本框中。
有了TextBlock,我没有这个问题,但是我需要文本框来显示我的文本。
解决方法
您可以尝试RichTextBox
。
当我想使用TextBox
时,在一定高度下我也会遇到同样的问题。我通过使用RichTextBox
而不是TextBox
解决了这个问题。
<Expander DataContext="{Binding DiagnosticCategories[0].DiagnosticResults[0]}" <!-- For the test -->
Background="Transparent"
Foreground="{StaticResource ActiveForegroundBrush}"
IsExpanded="False">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Top">
<RichTextBox
Margin="10"
Background="Transparent"
BorderThickness="0"
FontSize="13"
FontWeight="Light"
Foreground="{StaticResource ActiveForegroundBrush}"
IsReadOnly="True"
Opacity="0.8"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionBrush="Black"
Text="{Binding FormatedParameters,Mode=OneWay}"
TextWrapping="Wrap" />
</StackPanel>
[...]
</Grid>
</Expander>
,
正如其他人所提到的,我有点不确定您要达到的目标。我在您的示例中添加了一些水平和垂直布局:
<Expander DataContext="{Binding DiagnosticCategories[0].DiagnosticResults[0]}"
Background="Yellow" HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="200"
IsExpanded="False">
<Grid Margin="10" Background="Blue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="*" SharedSizeGroup="A" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Top">
<TextBox
Margin="10"
Background="Orange"
BorderThickness="0"
FontSize="13"
FontWeight="Light"
IsReadOnly="True"
Opacity="0.8"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectionBrush="Black"
Text="Test Text"
TextWrapping="Wrap"
/>
</StackPanel>
</Grid>
</Expander>
然后我得到以下信息:
关闭:
打开: