列表框内内切换按钮中的文本块Textwrapping的ItemTemplate [英] Textwrapping within Textblock within Togglebutton within ListBox ItemTemplate

查看:135
本文介绍了列表框内内切换按钮中的文本块Textwrapping的ItemTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现列表框项的切换按钮控件模板。这是在应用程序中,用户可以在列表框项点击,以显示一个特定的功能块也可以使用。

I'm trying to achieve a ToggleButton control template for listbox items. This is to be used in an application where the user can click on the listbox items to show a certain piece of functionality.

列表框项目模板,定义如下:

The listbox item template is defined as follows:

    <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>

和列表框被定义为

        <ListBox x:Name="_examplesListBox" 
                 SelectionMode="Single"
                 BorderBrush="Transparent"
                 Background="Transparent"
                 ItemsSource="{Binding AllExamples}" 
                 ItemContainerStyle="{StaticResource ExampleListBoxItemStyle}"
                 SelectedItem="{Binding SelectedExample, Mode=TwoWay}"/>

下面我有两个的TextBlocks,人们势必ExampleDisplayName,其他绑定到ExampleDescription。我想达到的效果是让第二个文本块(介绍)环绕,受可用空间的限制。

Here I have two textblocks, one bound to ExampleDisplayName, the other bound to ExampleDescription. The effect I am trying to achieve is to get the second textblock (description) to wrap around, constrained by the available space.

这就是我现在越来越:

我想是用于显示例如描述包裹的第二行基于列表框的大小。当应用程序启动列表框应自动调整大小到第一行+余量。

What I'd like is for the second line showing example description to wrap based on the size of the listbox. When the application starts the listbox should auto-size to the first line + margin.

有什么建议?

推荐答案

去除水平滚动条应该帮助文字换行:

Removing that horizontal scrollbar should help with text wrapping:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">



我不太清楚如何自动调整大小的ListBox 在启动的基础上只使用XAML第一个文本行的大小。

I'm not quite sure how to auto-size ListBox on startup based on first text line size using only XAML.

这篇关于列表框内内切换按钮中的文本块Textwrapping的ItemTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆