为什么忽略控件内部定义的Style?

时间:2015-08-27 12:16:31

标签: wpf wpf-4.0

我在ListBox中有一个UserControl,其样式在ListBox.Resources中定义,但样式被忽略。我们在ListBox中添加了通用Application.Resources样式。

为什么Application.Resources样式优先于控件资源中定义的样式?

这就是我定义ListBox及其风格的方式:

<ListBox x:Name="myListBox" SelectionMode="Extended" >            
    <ListBox.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>                                        
                                </Grid.ColumnDefinitions>
                                <Border Margin="0" Padding="2" Grid.Column="0" Grid.ColumnSpan="2" x:Name="Background" CornerRadius="0" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                                <CheckBox x:Name="myCheckBox" Grid.Column="0" IsChecked="{Binding MyBoolean}" 
                                      HorizontalAlignment="Center" IsThreeState="False" VerticalAlignment="Center" Background="Transparent" Click="myCheckBox_Click"/>
                                <TextBlock Grid.Column="1" Text="{Binding ItemName}" VerticalAlignment="Center" Margin="4,1,2,4">
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Setter Property="Foreground" Value="Black" />                                                
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.Resources>
</ListBox>

这是我们在ListBox - App.xaml中添加Application.Resources通用样式的方式:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>                         
            <ResourceDictionary Source="pack://application:,,,/MyAssembly;component/MyListBoxGenericStyle.xaml" />                
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

另外,如果我在ListBox.ItemContainerStyle中定义Style,它似乎有效:

<ListBox x:Name="myListBox" SelectionMode="Extended" >            
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>                                        
                                </Grid.ColumnDefinitions>
                                <Border Margin="0" Padding="2" Grid.Column="0" Grid.ColumnSpan="2" x:Name="Background" CornerRadius="0" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                                <CheckBox x:Name="myCheckBox" Grid.Column="0" IsChecked="{Binding MyBoolean}" 
                                      HorizontalAlignment="Center" IsThreeState="False" VerticalAlignment="Center" Background="Transparent" Click="myCheckBox_Click"/>
                                <TextBlock Grid.Column="1" Text="{Binding ItemName}" VerticalAlignment="Center" Margin="4,1,2,4">
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Setter Property="Foreground" Value="Black" />                                                
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.Resources>
</ListBox>

2 个答案:

答案 0 :(得分:0)

您应该定义字典下列表框资源下列出的样式。将样式或模板放在要操作的元素下类似于忽略CSS并直接在HTML中的元素中执行样式。

您可以为字典资源模板指定名称,并在标记中将其引用为

<ListBox ItemTemplate="{StaticResource MyTemplate}">

答案 1 :(得分:0)

检查 MyListBoxGenericStyle.xaml 的样式是否没有ItemContainerStyle明确设置为其他值。这会使您的默认样式无效。

此外,您可以通过Snoop检查您的ListBoxItem从哪里获取样式(即本地设置,资源,主题等)。

相关问题