Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Silverlight 2 solve a Layout Bug in ListBox

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how Silverlight 2 solves a Layout Bug in ListBox. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Is there anything wrong with Silverlight itself? No one can answer.

I encountered a problem about ListBox in my work. A brief description: use ListBox to display a collection of objects, and when typesetting, you find that no matter how you adjust the properties of ListBox, you can't make ListItem fill the whole space; frustratingly, the TextBlock/TextBox emitted in ListItem will automatically set its own length according to the size of its own text. All controls in ListItem are automatically aligned to the left, creating a "Gantt chart" image. Examples (name, age, email address) are as follows:

After setting the light blue Border, this phenomenon is too obvious!

According to MSDN, we only need to add the following setting statement to the attribute of ListBox to force the automatic Fill of length:

HorizontalContentAlignment= "Stretch"

But it has no effect after joining! This is obviously another bug of Silverlight 2.

We can see the default Style of ItemContainer on MSDN (you can also see it from here: http://msdn.microsoft.com/en-us/library/cc278062%28vs.95%29.aspx):

1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: Visible 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 52: 53: 54: 55: 56: 57:

As you can see, there are only two properties whose value is set to Left:

Line 3 HorizontalContentAlignment

Line 50 HorizontalAlignment

The problem lies in the Left in line 50, which by default arranges everything in a List Item in the way you want to align. Since the style is already written in the Silverlight Runtime, we can only rewrite the Style to remove the line and specify a new Style for the ListBox.

Solution:

Add the attribute HorizontalContentAlignment= "Stretch" for ListBox to force Fill

Add a namespace to App.xaml: xmlns:vsm= "clr-namespace:System.Windows;assembly=System.Windows"

Add Style without HorizontalAlignment= "Left" to App.xaml, and name its key ListBoxItemContainerStyle-x key key = "ListBoxItemContainerStyle"

Add the attribute ItemContainerStyle= "{StaticResource ListBoxItemContainerStyle} to the ListBox to specify a user-defined Style

Done!

Now you can see the result of this run:

The middle age part can automatically change the width according to the size of the form.

The above is how Silverlight 2 solves a Layout Bug in ListBox. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report