偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Silverlight 2解決ListBox中一個Layout Bug

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

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

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

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

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

HorizontalContentAlignment="Stretch"
但是加入之后沒有效果! 這顯然是Silverlight 2的又一個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è)置為Left的屬性僅有2個:

第3行 HorizontalContentAlignment

第50行 HorizontalAlignment

問題出在了第50行的這個Left,它默認(rèn)將一個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é)果了:

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

【編輯推薦】

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

2017-10-10 15:14:23

BUGiOS 11蘋果

2021-10-08 07:50:57

軟件設(shè)計程序

2025-02-13 07:00:00

Dubbo-goJava服務(wù)端

2014-12-17 09:40:22

dockerLinuxPaaS

2009-09-14 17:08:02

WebFormView

2024-04-22 00:00:01

Redis集群

2024-08-08 08:09:38

2012-07-09 09:31:53

silverlight

2009-08-26 17:53:31

C# DropDown

2011-08-01 09:25:32

SQL Server數(shù)

2011-03-03 21:04:08

bug程序員

2021-07-24 13:11:19

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

2015-08-24 10:07:13

程序員bug

2019-08-01 12:59:21

Bug代碼程序

2022-06-15 08:14:40

Go線程遞歸

2010-11-17 15:43:55

軟件測試Bug

2023-03-13 08:09:03

Protobuffeature分割

2022-05-16 08:42:26

Pandasbug

2021-09-13 08:41:52

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

2022-11-21 18:37:40

漏測BugSOP
點贊
收藏

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