2013-10-07 43 views
0

我已經做了一些研究,並且找到了爲每個ListBoxItem設置一個備用背景顏色的方法,但即使列表框爲空,我仍希望該背景可以繼續。替代背景列表框空行顏色?

<Style x:Key="ListBoxStyle" TargetType="ListBox"> 
    <Setter Property="Width" Value="288"></Setter> 
    <Setter Property="BorderBrush" Value="#D2D2D2"></Setter> 
    <Setter Property="BorderThickness" Value="1"></Setter> 
    <Setter Property="Margin" Value="1,0,1,0"></Setter> 
    <Setter Property="AlternationCount" Value="2"></Setter> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter> 
</Style> 

<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem"> 
    <Setter Property="Height" Value="27"></Setter> 
    <Setter Property="FontWeight" Value="Medium"/> 
    <Setter Property="FontSize" Value="12"/> 
    <Setter Property="Foreground" Value="#333333"/> 
    <Style.Triggers> 
     <Trigger Property="ItemsControl.AlternationIndex" Value="0"> 
      <Setter Property="Background" Value="White"></Setter> 
     </Trigger> 
     <Trigger Property="ItemsControl.AlternationIndex" Value="1"> 
      <Setter Property="Background" Value="#19000000"></Setter> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

是否可以在整個ListBox高度上設置替代背景?

回答

2

據我所知,這是項目Background,使這個,所以我會說,不,這是不可能的。我可以想象,你可以產生這種效果的唯一方法是將空項添加到ListBox。不過,我強烈建議你不要這樣做,因爲這些空的項目仍然可以選擇,這可能會混淆用戶。

+0

我唯一能做的就是不用創建新的控件,而是巧妙地將背景看做空的交替項目,但只有在事先知道項目高度的情況下才能做到這一點匹配滾動位置很可能是一場噩夢。 –

0

我之前通過創建兩個幾乎相同的控件並將它們重疊來完成此操作。底部控件具有完全相同的佈局屬性,並綁定到任意大的空行集合。同一個框的頂部版本綁定到實際的數據集。不同之處在於頂部控件的背景是透明的,所以一組空行顯示直到框填滿。這使得它以任何可預測的方式影響一切。

我有一些代碼隱藏,導致頂部框被選中,如果底部的任何觸摸。