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: