【问题标题】:Override application-wide style with a template in Silverlight在 Silverlight 中使用模板覆盖应用程序范围的样式
【发布时间】:2012-03-04 04:21:14
【问题描述】:

我有一个 Silverlight 应用程序,我在其中设置了列表框的样式。这是应用程序范围的 ListBoxItem 样式的一部分:

<VisualStateGroup x:Name="CommonStates">
    <VisualState x:Name="Normal" />
    <VisualState x:Name="MouseOver">
        <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ListBoxItemGrid" Storyboard.TargetProperty="Background" Duration="0">
                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MouseOverColorBrush}" />
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>
</VisualStateGroup>

现在我有另一个列表框,当用户将鼠标悬停在 ListBoxItem 上时,我不希望发生任何事情。我试过这个:

<VisualStateGroup x:Name="CommonStates">
    <VisualState x:Name="Normal" />
    <VisualState x:Name="MouseOver" />
</VisualStateGroup>

但这似乎不起作用。如何覆盖默认的应用程序范围的样式?我不确定它是否相关,但应用程序范围的样式是样式,而我的“特殊”列表框的 ListBoxItem 有一个模板。

【问题讨论】:

    标签: silverlight templates listbox styles listboxitem


    【解决方案1】:

    找到了。我必须给容器一个样式:

    <ListBox Grid.Row="1" 
        ItemsSource="{Binding TheItems}" 
        ItemContainerStyle="{StaticResource TheLineStyle}" 
        ItemTemplate="{StaticResource TheItemTemplate}"></ListBox>
    
    
    <Style TargetType="ListBoxItem" x:Key="TheLineStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid>
                        <ContentControl Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" />
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 2021-12-07
      • 2011-09-13
      • 1970-01-01
      • 2022-10-07
      • 2019-06-10
      • 2020-09-12
      • 2013-07-28
      • 1970-01-01
      相关资源
      最近更新 更多