自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Silverlight 2解決ListBox中一個(gè)Layout Bug

開發(fā) 后端
Silverlight 2 在其前一個(gè)版本的基礎(chǔ)上, 功能集及開發(fā)人員友好性方面有了較大的提升。毫無(wú)疑問, Silverlight自身的問題越來越少. 我想從事Silverlight開發(fā)的同行們, 尤其是老Silverlighter們肯定也有這樣的感受。

Silverlight自身還有沒有問題? 誰(shuí)也沒法回答.

工作中遇到了一個(gè)關(guān)于ListBox的問題. 簡(jiǎn)單描述一下: 使用ListBox來顯示某對(duì)象集合, 在排版的時(shí)候, 發(fā)現(xiàn)無(wú)論怎么調(diào)整ListBox的屬性, 都無(wú)法讓ListItem充滿整個(gè)空間; 令人郁悶的是,ListItem中排放的TextBlock/TextBox總會(huì)根據(jù)自身文本的大小, 自動(dòng)設(shè)定自己的長(zhǎng)度; ListItem中的所有控件都自動(dòng)向左對(duì)齊,造成了一副"甘特圖"式的圖像, 舉例(姓名, 年齡, 郵件地址)如下:

在設(shè)置了淺藍(lán)色的Border之后, 這個(gè)現(xiàn)象實(shí)在是太明顯了!

按照MSDN的說法, 我們只需要在ListBox的屬性中加入如下設(shè)定語(yǔ)句, 就會(huì)強(qiáng)制長(zhǎng)度自動(dòng)Fill了:

HorizontalContentAlignment="Stretch"
但是加入之后沒有效果! 這顯然是Silverlight 2的又一個(gè)bug.

我們可以在MSDN上看到ItemContainer的默認(rèn)Style(你也可以從這里看: http://msdn.microsoft.com/en-us/library/cc278062%28vs.95%29.aspx):

   1: <Style TargetType="ListBoxItem">
   2:   <Setter Property="Padding" Value="3" />
   3:   <Setter Property="HorizontalContentAlignment" Value="Left" />
   4:   <Setter Property="VerticalContentAlignment" Value="Top" />
   5:   <Setter Property="Background" Value="Transparent" />
   6:   <Setter Property="BorderThickness" Value="1"/>
   7:   <Setter Property="TabNavigation" Value="Local" />
   8:   <Setter Property="Template">
   9:     <Setter.Value>
  10:       <ControlTemplate TargetType="ListBoxItem">
  11:         <Grid Background="{TemplateBinding Background}">
  12:           <vsm:VisualStateManager.VisualStateGroups>
  13:             <vsm:VisualStateGroup x:Name="CommonStates">
  14:               <vsm:VisualState x:Name="Normal" />
  15:               <vsm:VisualState x:Name="MouseOver">
  16:                 <Storyboard>
  17:                   <DoubleAnimation Storyboard.TargetName="fillColor" Storyboard.TargetProperty="Opacity" Duration="0" To=".35"/>
  18:                 </Storyboard>
  19:               </vsm:VisualState>
  20:             </vsm:VisualStateGroup>
  21:             <vsm:VisualStateGroup x:Name="SelectionStates">
  22:               <vsm:VisualState x:Name="Unselected" />
  23:               <vsm:VisualState x:Name="Selected">
  24:                 <Storyboard>
  25:                   <DoubleAnimation Storyboard.TargetName="fillColor2" Storyboard.TargetProperty="Opacity" Duration="0" To=".75"/>
  26:                 </Storyboard>
  27:               </vsm:VisualState>
  28:             </vsm:VisualStateGroup>
  29:             <vsm:VisualStateGroup x:Name="FocusStates">
  30:               <vsm:VisualState x:Name="Focused">
  31:                 <Storyboard>
  32:                   <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Visibility" Duration="0">
  33:                     <DiscreteObjectKeyFrame KeyTime="0">
  34:                       <DiscreteObjectKeyFrame.Value>
  35:                         <Visibility>Visible</Visibility>
  36:                       </DiscreteObjectKeyFrame.Value>
  37:                     </DiscreteObjectKeyFrame>
  38:                   </ObjectAnimationUsingKeyFrames>
  39:                 </Storyboard>
  40:               </vsm:VisualState>
  41:               <vsm:VisualState x:Name="Unfocused"/>
  42:             </vsm:VisualStateGroup>
  43:           </vsm:VisualStateManager.VisualStateGroups>
  44:           <Rectangle x:Name="fillColor" Opacity="0" Fill="#FFBADDE9" IsHitTestVisible="False" RadiusX="1" RadiusY="1"/>
  45:           <Rectangle x:Name="fillColor2" Opacity="0" Fill="#FFBADDE9" IsHitTestVisible="False" RadiusX="1" RadiusY="1"/>
  46:           <ContentPresenter
  47:             x:Name="contentPresenter"
  48:             Content="{TemplateBinding Content}"
  49:             ContentTemplate="{TemplateBinding ContentTemplate}"
  50:             HorizontalAlignment="Left"
  51:             Margin="{TemplateBinding Padding}"/>
  52:           <Rectangle x:Name="FocusVisualElement" Stroke="#FF45D6FA" StrokeThickness="1" Visibility="Collapsed" RadiusX="1" RadiusY="1" />
  53:         </Grid>
  54:       </ControlTemplate>
  55:     </Setter.Value>
  56:   </Setter>
  57: </Style>


可以看出來, 值設(shè)置為L(zhǎng)eft的屬性僅有2個(gè):

第3行 HorizontalContentAlignment

第50行 HorizontalAlignment

問題出在了第50行的這個(gè)Left,它默認(rèn)將一個(gè)List Item中的所有內(nèi)容都按照想做對(duì)齊的方式排列,由于這個(gè)style已經(jīng)寫在了Silverlight Runtime內(nèi),所以我們只能重寫這個(gè)Style去掉這一行并為L(zhǎng)istBox指定新的Style。

解決方法:

為L(zhǎng)istBox添加屬性 HorizontalContentAlignment="Stretch", 強(qiáng)制Fill

在App.xaml中添加命名空間: xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"

在App.xaml中添加去掉了HorizontalAlignment="Left"的Style, 并給它的key命名為L(zhǎng)istBoxItemContainerStyle ---x:Key="ListBoxItemContainerStyle"

為L(zhǎng)istBox添加屬性 ItemContainerStyle="{StaticResource ListBoxItemContainerStyle}, 指定用戶自定義Style
Done!

現(xiàn)在你可以看到這個(gè)運(yùn)行結(jié)果了:

 中間的年齡部分是可以隨著窗體大小變化自動(dòng)變化寬度的.

【編輯推薦】

  1. 在Silverlight中進(jìn)行基本的數(shù)據(jù)驗(yàn)證
  2. 詳解Silverlight中的Downloader對(duì)象
  3. Silverlight 2 Twitter 例程
責(zé)任編輯:彭凡 來源: cnblog
相關(guān)推薦

2017-10-10 15:14:23

BUGiOS 11蘋果

2021-10-08 07:50:57

軟件設(shè)計(jì)程序

2009-09-14 17:08:02

WebFormView

2024-04-22 00:00:01

Redis集群

2014-12-17 09:40:22

dockerLinuxPaaS

2025-02-13 07:00:00

Dubbo-goJava服務(wù)端

2024-08-08 08:09:38

2012-07-09 09:31:53

silverlight

2011-08-01 09:25:32

SQL Server數(shù)

2009-08-26 17:53:31

C# DropDown

2021-07-24 13:11:19

Redis數(shù)據(jù)技術(shù)

2011-03-03 21:04:08

bug程序員

2014-07-07 10:58:22

SQL Server

2018-11-22 15:50:27

MySQL數(shù)據(jù)庫(kù)雙引號(hào)

2023-11-23 16:46:55

LinuxAWK運(yùn)維

2021-09-13 08:41:52

職場(chǎng)互聯(lián)網(wǎng)自閉

2022-05-16 08:42:26

Pandasbug

2022-11-21 18:37:40

漏測(cè)BugSOP

2022-06-15 08:14:40

Go線程遞歸

2023-03-13 08:09:03

Protobuffeature分割
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)