简体   繁体   English

WP7 ListBox ItemContainerStyle XAML禁用无效

[英]WP7 ListBox ItemContainerStyle XAML disabled not working

I have the following style and list box: 我有以下样式和列表框:

<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}" />

I used Expression Blend to create the style. 我使用Expression Blend来创建样式。 I want the ListBoxItem to have a 60% opacity when disabled. 我希望ListBoxItem在禁用时具有60%的不透明度。 I'm populating the ListBox programatically with ListBoxItems that have their IsEnabled property set based on certain criteria. 我正在使用基于特定条件设置了IsEnabled属性的ListBoxItems以编程方式填充ListBox。 I've stepped through the debugger and confirmed that the ListBoxItems do have IsEnabled = false, so my conclusion is that there must be something wrong with my xaml. 我已经通过调试器并确认ListBoxItems确实有IsEnabled = false,所以我的结论是我的xaml肯定有问题。 Is there something I'm missing or doing wrong that is causing the items to not become opaque when disabled? 是否有一些我错过或做错的事情导致物品在禁用时不会变得不透明?

The ListBox is on a white background and has black text as the content. ListBox在白色背景上,并以黑色文本作为内容。 The opacity should make it gray. 不透明度应该使它变灰。 If I add opacity to the normal visual state, it shows up as intended for the normal state, but also for the Disabled state. 如果我将opacity添加到正常的可视状态,它会显示为正常状态,但也显示为Disabled状态。 I know the disabled items are actually disabled because I can't click on them. 我知道禁用的项目实际上是禁用的,因为我无法点击它们。 I figured that the code below would show normal state as opaque but disabled items without opacity. 我认为下面的代码将正常状态显示为不透明但禁用的项目没有不透明度。

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

Update: I'm pretty sure there is something wrong with my disabled state. 更新:我很确定我的残疾人状态有问题。 Nothing I add in the disabled state takes hold, even if I change the background to blue. 即使我将背景更改为蓝色,我在禁用状态下添加的任何内容都将保留。 I am creating ListBoxItems programatically and setting the content property to a user control I have created. 我以编程方式创建ListBoxItems并将content属性设置为我创建的用户控件。 Could this be causing problems? 这可能导致问题吗? It doesn't make sense to me because I can set the normal state with 60% opacity and it works, so why wouldn't the disabled state? 这对我来说没有意义,因为我可以设置60%不透明度的正常状态并且它可以工作,那么为什么禁用状态呢?

You should not be using a ContentControl inside your control template. 您不应该在控件模板中使用ContentControl。 You should be using a ContentPresenter instead. 您应该使用ContentPresenter。 You can run into weird issues by having a ContentControl displaying the content of another ContentControl. 您可以通过让ContentControl显示另一个ContentControl的内容来遇到奇怪的问题。

Aside from that, the ListBoxItem will only go into the "Disabled" state, if the ListBoxItem.Content is not a Control. 除此之外,如果ListBoxItem.Content不是Control,ListBoxItem将只进入“Disabled”状态。 If the ListBoxItem.Content is a Control, then it will transition to the "Normal" state even if ListBoxItem.IsEnabled is false. 如果ListBoxItem.Content是一个Control,那么即使ListBoxItem.IsEnabled为false,它也将转换为“Normal”状态。

You've created a style for the ListBoxItem, but not for the ListBox itself. 您已经为ListBoxItem创建了一个样式,但是没有为Lis​​tBox本身创建样式。 And you don't have to, necessarily. 并且你没有必要。 The problem is that by default the ListBox has a white background. 问题是默认情况下ListBox有一个白色背景。

So the first step is to set the ListBox background to Transparent like this... 所以第一步是将ListBox背景设置为像这样的透明...

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

Then I just made a couple changes to your ListBoxItem style... 然后我对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>

And now as long as the container around the ListBox is not white, then a disabled ListBoxItem should appear translucent thanks to the Opacity setting. 现在,只要ListBox周围的容器不是白色,由于不透明度设置,禁用的ListBoxItem应该显示为半透明。

For example... 例如...

<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>

Will look like this... 看起来像这样......

残疾人风格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM