![](/img/trans.png)
[英]How to align the text in textbox, seems different character has different width
[英]Align Text Width
如何將 TextBlock 的“Hello World”文本限制為 ItemsControl 的高度? 這樣,如果它不適合,它會顯示字符省略號
即如果我有 3 個項目,我希望它看起來像這樣(文本被修剪):
4 個項目(文本仍被修剪):
這是我的代碼:
<Border BorderBrush="Black"
HorizontalAlignment="Center"
VerticalAlignment="Center"
BorderThickness="1">
<Grid HorizontalAlignment="Center">
<TextBlock Text="Hello World!"
TextTrimming="CharacterEllipsis"
FontSize="20"
Margin="-30,0,0,0">
<TextBlock.LayoutTransform>
<RotateTransform Angle="-90" />
</TextBlock.LayoutTransform>
</TextBlock>
<ItemsControl ItemsSource="{Binding Items}"
Width="100"
x:Name="other" />
</Grid>
</Border>
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ObservableCollection<string> Items { get; } = new ObservableCollection<string>() { "A", "B", "C" };
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.Key == Key.Up)
{
Items.Add("NEW STRING");
}
else if (e.Key == Key.Down)
{
if (Items.Any())
Items.RemoveAt(Items.Count - 1);
}
}
}
我嘗試做的一件事是將 TextBlock 綁定到 ItemsControl 的高度:
<TextBlock Text="Hello World!"
TextTrimming="CharacterEllipsis"
FontSize="20"
Margin="-30,0,0,0"
Width="{Binding ActualHeight, ElementName=other}">
<TextBlock.LayoutTransform>
<RotateTransform Angle="-90" />
</TextBlock.LayoutTransform>
</TextBlock>
但這不僅不起作用,而且當您刪除項目時它也不會降低高度(按向下鍵)
結果我只需要將 ItemsControl 設置為對齊“頂部”而不是默認的“拉伸”
盡管我不確定此解決方案中是否存在在某些情況下可能會中斷的循環依賴。
<DockPanel VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBlock Text="Hello World!"
TextTrimming="CharacterEllipsis"
FontSize="20"
DockPanel.Dock="Left"
Width="{Binding ActualHeight, ElementName=other}">
<TextBlock.LayoutTransform>
<RotateTransform Angle="-90" />
</TextBlock.LayoutTransform>
</TextBlock>
<ItemsControl ItemsSource="{Binding Items}"
Background="Red"
VerticalAlignment="Top"
Width="100"
x:Name="other" />
</DockPanel>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.