2011-02-15 36 views
3

我有以下樣式和列表框:WP7列表框ItemContainerStyle XAML禁用不工作

<Style x:Key="LwHListBoxItemStyle" TargetType="ListBoxItem"> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="BorderBrush" Value="Transparent"/> 
    <Setter Property="Padding" Value="0"/> 
    <Setter Property="HorizontalContentAlignment" Value="Left"/> 
    <Setter Property="VerticalContentAlignment" Value="Top"/> 
    <Setter Property="Padding" Value="24, 0, 24, 0" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <Border x:Name="LayoutRoot" BorderBrush="#FFCCCCCC" BorderThickness="0, 0, 0, 1" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="MouseOver"/> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/> 
            <DoubleAnimation Duration="0" To="0.6" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="LayoutRoot" d:IsOptimized="True"/> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<ListBox x:Name="lbxContainer" Height="Auto" Width="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalAlignment="Top" ItemContainerStyle="{StaticResource LwHListBoxItemStyle}" /> 

我使用Expression Blend中創建的風格。我想讓ListBoxItem在禁用時具有60%的不透明度。我使用ListBoxItems以編程方式填充ListBox,並根據特定條件設置其IsEnabled屬性。我已經通過調試器,並確認ListBoxItems確實有IsEnabled = false,所以我的結論是,我的xaml一定有問題。有什麼我失蹤或做錯了,導致項目禁用時不會變得不透明?

列表框是一個白色背景,並有黑色文本的內容。不透明度應該使它變灰。如果我將不透明度添加到正常視覺狀態,它會按照預期的正常狀態顯示,但也會顯示爲禁用狀態。我知道禁用的項目實際上已被禁用,因爲我無法點擊它們。我認爲下面的代碼會顯示正常狀態爲不透明,但禁用了不透明的項目。

<VisualState x:Name="Normal"> 
    <Storyboard> 
     <DoubleAnimation Duration="0" To="0.6" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="LayoutRoot" d:IsOptimized="True"/> 
    </Storyboard> 
</VisualState> 

更新:我很確定我的禁用狀態有問題。即使我將背景更改爲藍色,我在禁用狀態下添加的任何東西都會佔據上風。我以編程方式創建ListBoxItems並將content屬性設置爲我創建的用戶控件。這可能導致問題嗎?這對我來說沒有任何意義,因爲我可以用60%的不透明度設置正常狀態,它可以工作,那麼爲什麼不能禁用狀態?

回答

0

您已經創建了一個ListBoxItem的樣式,而不是列表框本身。而且你不一定必須。問題是,默認情況下,ListBox具有白色背景。

所以第一步是設置列表框背景爲透明這樣的... ...

<ListBox x:Name="lbxContainer" Background="Transparent" Height="Auto" Width="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalAlignment="Top" ItemContainerStyle="{StaticResource LwHListBoxItemStyle}" /> 

然後,我只是做了一些更改,以你的風格ListBoxItem的...

<Style x:Key="LwHListBoxItemStyle" TargetType="ListBoxItem"> 
    <Setter Property="Background" Value="White"/> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="BorderBrush" Value="Transparent"/> 
    <Setter Property="Padding" Value="0"/> 
    <Setter Property="HorizontalContentAlignment" Value="Left"/> 
    <Setter Property="VerticalContentAlignment" Value="Top"/> 
    <Setter Property="Padding" Value="24, 0, 24, 0" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <Border x:Name="LayoutRoot" BorderBrush="#FFCCCCCC" Background="{TemplateBinding Background}" BorderThickness="0, 0, 0, 1" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="MouseOver"/> 
          <VisualState x:Name="Disabled"> 
           <Storyboard>            
            <DoubleAnimation Duration="0" To="0.6" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="LayoutRoot" /> 
            <DoubleAnimation Duration="0" To="0.6" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

而且現在只要ListBox周圍的容器不是白色的,那麼由於不透明度設置,禁用的ListBoxItem應該顯示爲半透明。

例如...

<Grid Background="Black"> 
    <ListBox x:Name="lbxContainer" Background="Transparent" Height="Auto" Width="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalAlignment="Top" ItemContainerStyle="{StaticResource LwHListBoxItemStyle}"> 
     <ListBoxItem Content="enabled a" /> 
     <ListBoxItem Content="disabled b" IsEnabled="False"/> 
     <ListBoxItem Content="enabled c"/> 
    </ListBox> 
</Grid> 

看起來就像這樣......

disabled style

+0

謝謝,但是這並不完全是我要找的。列表框位於白色背景上,並具有黑色文字作爲內容。不透明度應該使它變灰。如果我將不透明度添加到正常視覺狀態,它會按預期顯示。我不知道爲什麼它不適用於殘疾人士。<的VisualState X:名稱= 「正常」> \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t CACuzcatlan

1

你不應該使用控件模板內ContentControl中。您應該改用ContentPresenter。您可以通過讓ContentControl顯示其他ContentControl的內容來解決奇怪的問題。

除此之外,在ListBoxItem的只會進入「禁用」狀態,如果ListBoxItem.Content不是控制。如果ListBoxItem.Content是一個Control,那麼即使ListBoxItem.IsEnabled爲false,它也將轉換爲「Normal」狀態。