WPF: textblock. texttrimming in the horizontal stackpanel of listview fails.

Source: Internet
Author: User

In many usage scenarios, we need to place something horizontally in the columns of the listview, so we use a horizontal stackpanel, but the problem is that texttrimming of textblock fails under the horizontal stackpanel. The reason is that the horizontal stackpanel does not limit the width of the space, so texttrimming, including textwrapping, does not work at this time, and the textblock will always be long. Unless you manually display the textblock width, this is obviously not a good method.

 

For example, this Code:

<Window. Resources>

<! -- Data -->

<X: arrayextension X: Key = "arr"

Xmlns = "CLR-namespace: system; Assembly = mscorlib"

Type = "string">

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

No ellipsis will appear in the result:

 

Of course, the vertical stackpanel does not have this problem (The vertical stackpanel limits the width without the height ). The solution is not to use stackpanel. Do not forget to plan the huge "Grid" in the control in WPF ".

Change the celltemplate of gridviewcolumn to 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>

 

Result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.