WPF:ListView中橫向StackPanel內的TextBlock.TextTrimming失靈

來源:互聯網
上載者:User

很多使用,我們需要在ListView中的列中橫向擺放一些東西,因此用到橫向的StackPanel,但是問題是在橫向的StackPanel下TextBlock的TextTrimming會失靈。原因則是橫向的StackPanel橫向不會限制空間寬度,那麼此時TextTrimming包括TextWrapping都不會起作用的,TextBlock永遠都會要多長就有多長。除非手動顯示TextBlock的寬度,顯然這不是一個好辦法。

 

比如這個代碼:

<Window.Resources>

    <!-- 資料 -->

    <x:ArrayExtension x:Key="arr"

                     xmlns="clr-namespace:System;assembly=mscorlib"

                     Type="String">

        <String>hehe long long long long long long long</String>

        <String>12345678900-78976587865</String>

    </x:ArrayExtension>

</Window.Resources>

<ListView ItemsSource="{StaticResource arr}">

    <ListView.View>

        <GridView>

            <GridViewColumn Header="aaa"

                           Width="50">

                <GridViewColumn.CellTemplate>

                    <DataTemplate>

                        <StackPanel Orientation="Horizontal">

                            <Ellipse Width="10" Height="10" Fill="Navy"

                                    Margin="5"/>

                            <TextBlock Text="{Binding}"

                                      TextTrimming="CharacterEllipsis"/>

                        </StackPanel>

                    </DataTemplate>

                </GridViewColumn.CellTemplate>

            </GridViewColumn>

        </GridView>

    </ListView.View>

</ListView>

結果不會出現省略符號:

 

當然縱向的StackPanel不會出現這樣的問題(縱向的StackPanel限制寬度,不限制高度)。解決方案就是不用StackPanel,別忘了WPF中規劃控制項中的巨無霸“Grid”。

把上面的GridViewColumn的CellTemplate改成Grid實現:

<GridViewColumn.CellTemplate>

    <DataTemplate>

        <Grid>

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="Auto"/>

                <ColumnDefinition/>

            </Grid.ColumnDefinitions>

            <Ellipse Width="10"

                    Height="10"

                    Fill="Navy"

                    Margin="5"/>

            <TextBlock Text="{Binding}"

                      Grid.Column="1"

                      TextTrimming="CharacterEllipsis"/>

        </Grid>

    </DataTemplate>

</GridViewColumn.CellTemplate>

 

結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.