![](/img/trans.png)
[英]TextTrimming property of TextBlock placed in TreeViewItem using GridSplitter
[英]WPF TextBlock TextTrimming Not Working Inside a Grid With GridSplitter
我的XAML代碼具有以下結構:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" MinWidth="200"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" MinWidth="100"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto" MinWidth="150"/>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding MessagesCollectionView}" Margin="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Label Foreground="Black" FontSize="14">
<TextBlock Text="{Binding Title}" TextTrimming="CharacterEllipsis"/>
</Label>
<Label Foreground="Gray" FontSize="12">
<TextBlock Text="{Binding Content}" TextTrimming="CharacterEllipsis"/>
</Label>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<GridSplitter Width="5" Grid.Column="1" HorizontalAlignment="Stretch"/>
<!--More stuff here......-->
我在這里簡短介紹了XAML代碼,否則它使整個問題變得很混亂。 我按此順序從左到右有5列: ListBox
, GridSplitter
, RichTextBox
, GridSplitter
, ListView
。 我僅顯示前兩列,因為其余內容幾乎相同。
當我加載程序時, TextBoxes
從未顯示任何省略號。 我什至嘗試給ListBox
設置固定大小。
有誰知道如何修復顯示的省略號? 我相信這可能涉及到在有GridSplitter
時如何通知Grid
中的控件的大小。
我的目標是一個UI,其中中間視圖具有* width
,而左視圖和右視圖最初是固定大小的,但如果用戶願意,可以拉伸。
我不確定,如果您尚未找到解決方案,但是我認為以下內容可以為您提供幫助(請參見我的內聯評論):
<Window x:Class="SO40013780.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"
xmlns:local="clr-namespace:SO40013780"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="200">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox Name="TheList" Grid.Column="0">
<ListBox.Template>
<!-- Override the default controll template for the listbox -->
<ControlTemplate TargetType="{x:Type ListBox}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="True">
<!-- OBS: HorizontalScrollBarVisibility="Disabled" is the CHANGE -->
<ScrollViewer Focusable="False" Padding="{TemplateBinding Padding}" HorizontalScrollBarVisibility="Disabled">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</ScrollViewer>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" TargetName="Bd" Value="White"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="#FFD9D9D9"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsGrouping" Value="True"/>
<Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis" Background="Yellow" Foreground="Red" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<GridSplitter Width="5" HorizontalAlignment="Center" VerticalAlignment="Stretch" Grid.Column="1" />
<Border Background="Blue" Grid.Column="2" />
</Grid>
</Window>
就像我最初想到的那樣,它與列和拆分器無關。
上面xaml中的魔術是模板中滾動查看器上的HorizontalScrollBarVisibility =“ Disabled”。 使用此設置,將無法在列表框中進行水平滾動,因此,當列表框變得比項目中的文本窄時,內聯文本塊中的文本修剪將接管。
閱讀Henrik Hansen的答案后,我意識到解決方案非常簡單,只是ListBox
上的簡單代碼行。
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
這樣可以防止內部控件獲取所需的所有寬度,而將它們限制為列的寬度。
在內容控件中更改文本屬性時,省略號不會更改。 我檢查了ms文檔。 為了做到這一點,您應該編寫一個屬性並將TextTrimProperty綁定到該屬性(在視圖模型中執行邏輯)或為TextBlock編寫一個轉換器,或者您可以編寫自己的TextBlock類並為textblock文本屬性創建一個回調方法。在其中操作文本。 正如我告訴您的,更改文本后不會觸發texttrimming。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.