[英]WPF TextBlock.TextTrimming Not Working With Auto-sized ColumnDefinition
[英]How to prevent growing of TextBox in WPF auto-sized window
我有這個 WPF 應用程序,它顯示一個 label、一個按鈕和一個文本框,如下所示:
通過按鈕添加三行后,windwos 開始隨着文本框增長:
這是 XAML:
<Window x:Class="_99_TestWPFApp.MainWindow"
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"
mc:Ignorable="d"
Title="Simple WPF Test App" Height="Auto" Width="Auto" SizeToContent="WidthAndHeight">
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="A LABEL" "/>
<Button Grid.Row="1" Grid.Column="0" x:Name="btnAdd" Click="btnAdd_Click" Content="ADD LINE" />
<TextBox Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" VerticalScrollBarVisibility="Visible" Width="150"/>
</Grid>
為了將文本框的高度綁定到網格的高度,我還將它添加到文本框Height="{Binding ElementName=MainGrid, Path=ActualHeight}"
但沒有效果。
我希望在文本框通過滾動條顯示其內容時修復 window 和網格大小
這甚至可能嗎? 還是我必須在某處設置固定高度(例如窗戶)?
這實際上是可能的:經過一些實驗,我找到了一種可能的解決方案:
<Window x:Class="_99_TestWPFApp.MainWindow"
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"
mc:Ignorable="d"
Title="Simple WPF Test App" Height="Auto" Width="Auto" SizeToContent="WidthAndHeight">
<Grid x:Name="OuterGrid">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid x:Name="LeftInnerGrid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="A LABEL"/>
<Button Grid.Row="1" Grid.Column="0" x:Name="btnAdd" />
</Grid>
<!-- notice the additional grid and the binding of the height here -->
<Grid Name="RightInnerGrid" Grid.Row="0" Grid.Column="1" Height="{Binding ElementName=OuterGrid, Path=ActualHeight}">
<TextBox VerticalScrollBarVisibility="Visible" Width="150"/>
</Grid>
</Grid>
它將文本框添加到自己的網格中,並將內部網格的高度屬性綁定到外部網格的高度,從而達到了目的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.