溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Silverlight 2是怎么解決ListBox中一個Layout Bug

發(fā)布時間:2021-12-07 16:03:01 來源:億速云 閱讀:109 作者:柒染 欄目:編程語言

本篇文章為大家展示了Silverlight 2是怎么解決ListBox中一個Layout Bug,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

Silverlight自身還有沒有問題? 誰也沒法回答.

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

Silverlight 2是怎么解決ListBox中一個Layout Bug

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

按照MSDN的說法, 我們只需要在ListBox的屬性中加入如下設(shè)定語句, 就會強制長度自動Fill了:

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

我們可以在MSDN上看到ItemContainer的默認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è)置為Left的屬性僅有2個:

第3行 HorizontalContentAlignment

第50行 HorizontalAlignment

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

解決方法:

為ListBox添加屬性 HorizontalContentAlignment="Stretch", 強制Fill

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

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

為ListBox添加屬性 ItemContainerStyle="{StaticResource ListBoxItemContainerStyle}, 指定用戶自定義Style
Done!

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

Silverlight 2是怎么解決ListBox中一個Layout Bug

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

上述內(nèi)容就是Silverlight 2是怎么解決ListBox中一個Layout Bug,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI