Dr.*_*ABT 2 c# wpf layout xaml
我正在尝试为列表框项目实现 ToggleButton 控件模板。这将用于用户可以单击列表框项目以显示特定功能的应用程序中。
列表框项模板定义如下:
<Style x:Key="ExampleListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ToggleButton IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch">
<StackPanel Orientation="Vertical">
<TextBlock x:Name="ExampleTitle" Grid.Row="0" Foreground="#333333"
FontFamily="pack://application:,,,/Resources/Fonts/#Neuropol Regular"
FontSize="16" Height="26" TextAlignment="Left" HorizontalAlignment="Left"
VerticalAlignment="Top" Text="{Binding ExampleDisplayName}"
Margin="5"></TextBlock>
<TextBlock Grid.Row="1" Foreground="#333333" Margin="5,-5,5,3" HorizontalAlignment="Stretch"
TextAlignment="Left" FontFamily="Verdana" VerticalAlignment="Top"
TextWrapping="Wrap" Text="{Binding ExampleDescription}"/>
</StackPanel>
</ToggleButton>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
并且列表框被定义为
<ListBox x:Name="_examplesListBox"
SelectionMode="Single"
BorderBrush="Transparent"
Background="Transparent"
ItemsSource="{Binding AllExamples}"
ItemContainerStyle="{StaticResource ExampleListBoxItemStyle}"
SelectedItem="{Binding SelectedExample, Mode=TwoWay}"/>
Run Code Online (Sandbox Code Playgroud)
这里我有两个文本块,一个绑定到 ExampleDisplayName,另一个绑定到 ExampleDescription。我试图实现的效果是让第二个文本块(描述)环绕,受可用空间的限制。
这就是我现在得到的:

我想要的是显示示例描述的第二行,以根据列表框的大小进行换行。当应用程序启动时,列表框应自动调整为第一行 + 边距。
有什么建议?
删除水平滚动条应该有助于文本换行:
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
Run Code Online (Sandbox Code Playgroud)
我不太确定如何ListBox仅使用 XAML 根据第一个文本行大小在启动时自动调整大小。