WinRT水平菜单自动调整文本块的宽度,因此不会被截断

时间:2014-08-25 15:32:53

标签: wpf xaml windows-8 windows-runtime winrt-xaml

我正在尝试(在WinRT Universal App上)拥有一个没有截断文本的顶级菜单。 我从我的服务器获取我的项目并将其显示给我的用户,问题是我显示的项目有4个字符,有些有10个(或中间)。所以有些被截断了: enter image description here 我想要的是有一个可以自动重新调整大小的文本块,这样这个词就不会被截断,有人知道怎么做吗?

这是我的XAML代码:

<GridView  ItemsSource="{Binding Channels}" 
            SelectionMode="None"
            IsRightTapEnabled="False"
            IsSwipeEnabled="False"
            IsItemClickEnabled="True"
             Margin="5">
            <GridView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Style="{StaticResource ChannelMenuHyperButtonStyle}" 
                                Text="{Binding Name}"                                    
                                Margin="10,0,10,0">
                        </TextBlock>                        
                </DataTemplate>
            </GridView.ItemTemplate>

            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapGrid Orientation="Vertical"  />
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
        </GridView>

样式代码:

  <Style x:Key="ChannelMenuHyperButtonStyle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="{StaticResource DmBlueBrush}" />
        <Setter Property="FontWeight" Value="Normal" />
        <Setter Property="FontSize" Value="18" />
        <Setter Property="Padding" Value="5,5,5,5" />
    </Style>

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

由于您只需要一行项目,我建议使用 VirtualizingStackPanel 作为 ItemsPanel (而不是WrapGrid)。不要忘记将它的方向设置为水平方向。

Stackpanel也可以使用,但如果你的类别数量非常多,它可能会在内存中变重。

为什么呢?因为GridViews和Listviews使用WrapGrid作为默认的ItemsPanel,所以WrapGrid中的所有项目都具有相同的大小(第一个项目的大小将应用于所有项目)。它们具有此限制,因为它们是虚拟化面板,可以自动在多行/列中布置它们的项目。

答案 1 :(得分:-1)

我认为问题在于你同时应用了保证金和填充。对于具有超过6个字符的标题,重叠填充/边距会“截断”您的数据。

考虑从Style“ChannelMenuHyperButtonStyle”中的水平线移除Padding值。

<Style x:Key="ChannelMenuHyperButtonStyle" TargetType="TextBlock">
    <Setter Property="Foreground" Value="{StaticResource DmBlueBrush}" />
    <Setter Property="FontWeight" Value="Normal" />
    <Setter Property="FontSize" Value="18" />
    <Setter Property="Padding" Value="0,5,0,5" />
</Style>

记住边距是从边界外部创造空间,填充是从边界内部创造空间。

相关问题