2015-08-27 42 views
0

我在UserControl内有ListBox,其样式在​​中定义,但样式被忽略。我们在Application.Resources中添加了一个通用ListBox样式。为什么在控件内定义的样式被忽略?

为什么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里面好像有效:

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

你能从MyListBoxGenericStyle共享ListBox风格吗? – VMaleev

+0

@VMaleev - 当然,我刚刚添加了它。现在这很简单。 – Dzyann

回答

0

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

你可以给字典资源模板的名称,并在标签中引用它作为

<ListBox ItemTemplate="{StaticResource MyTemplate}"> 
0

检查MyListBoxGenericStyle.xaml的风格并没有明确ItemContainerStyle设置为其他值。这会使你的默认样式无用。

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

相关问题