2013-11-07 3 views
1

У меня проблема, что я не могу установить цвет фона моего ListBox-Control. Создать шаблон и DataTemplates ItemsControl:WPF ListBox не может установить BackgroundColor

<Style TargetType="ItemsControl" x:Key="LogViewerStyle"> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate> 
     <Grid> 
      <ScrollViewer CanContentScroll="True"> 
      <ItemsPresenter SnapsToDevicePixels="True" /> 
      </ScrollViewer> 
     </Grid> 
     </ControlTemplate> 
    </Setter.Value>  
    </Setter> 

<DataTemplate DataType="{x:Type local:LogEntry}"> 
    <Grid IsSharedSizeScope="True"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition SharedSizeGroup="Index" Width="Auto"/> 
     <ColumnDefinition SharedSizeGroup="Date" Width="Auto"/> 
     <ColumnDefinition/> 
    </Grid.ColumnDefinitions> 

    <TextBlock Text="{Binding Index}" Grid.Column="0" FontWeight="Normal" Margin="2,0,2,0" Foreground="{Binding Path=LineNumbersColor, ElementName=LogViewerProperty}" Cursor="RightArrow.cur" TextAlignment="Right" /> 
    <TextBlock Text="{Binding DateTime}" Grid.Column="1" FontWeight="Bold" Margin="0,0,5,0" /> 
    <TextBlock Text="{Binding Message}" Grid.Column="2" TextWrapping="{Binding Path=WordWrapping, ElementName=LogViewerProperty, Converter={StaticResource BoolToTextWrap}}" /> 

    </Grid> 
</DataTemplate> 

Когда я пытаюсь дать свою ListBox BackgroundColor ничего не происходит:

<ListBox ItemsSource="{Binding}" x:Name="LogViewer" Background="Cornsilk" Style="{StaticResource LogViewerStyle}"> 
    <ItemsControl.Template> 
    <ControlTemplate> 
     <ScrollViewer CanContentScroll="True" Padding="{TemplateBinding Padding}" HorizontalScrollBarVisibility="{Binding Path=WordWrapping, ElementName=LogViewerProperty, Converter={StaticResource BoolToScrollbarVisibility}}" VerticalScrollBarVisibility="{Binding Path=VerticalScrollbarVisible, ElementName=LogViewerProperty}"> 
     <ItemsPresenter/> 
     </ScrollViewer> 
    </ControlTemplate> 
    </ItemsControl.Template> 
    <ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <VirtualizingStackPanel IsItemsHost="True"> 
     </VirtualizingStackPanel> 
    </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ListBox> 

На данный момент я не имею понятия, почему. Может ли кто-нибудь дать мне подсказку? Благодаря!

ответ

2

Вы применяете стиль к ListBox с помощью ключа LogViewerStyle, который более точно определяет шаблон, но затем вы создаете другой шаблон для ListBox неявно.

Почему? Это не обычный хлеб wpf. Это не значит, что это?

Удалите из них один из них.

Хотя, чтобы ответить на ваш вопрос, вам нужно будет сказать, что ScrollViewer прослушивает фон списка ListBox.

Взгляните на это:

<Style TargetType="ListBox" x:Key="MyListBox"> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate> 
     <Grid Background="{TemplateBinding Background}"> 
      <ScrollViewer CanContentScroll="True"> 
      <ItemsPresenter SnapsToDevicePixels="True" /> 
      </ScrollViewer> 
     </Grid> 
     </ControlTemplate> 
    </Setter.Value>  
    </Setter> 

А потом просто установить стиль на ListBox.

<ListBox Style="{StaticResource MyListBox}" /> 

Посмотрите, как он сообщает, что Grid имеет свой фон, такой же, как у ListBox.

+0

Спасибо! Это оно. – simmeone

0

Установка фона = "{TemplateBinding фона}" относится фон для меня

<ListBox ItemsSource="{Binding}" Width="100" x:Name="LogViewer" Background="Red" Style="{StaticResource LogViewerStyle}"> 
        <ItemsControl.Template> 
         <ControlTemplate> 
          <ScrollViewer CanContentScroll="True" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" HorizontalScrollBarVisibility="{Binding Path=WordWrapping, ElementName=LogViewerProperty, Converter={StaticResource BoolToScrollbarVisibility}}" VerticalScrollBarVisibility="{Binding Path=VerticalScrollbarVisible, ElementName=LogViewerProperty}"> 
           <ItemsPresenter/> 
          </ScrollViewer> 
         </ControlTemplate> 
        </ItemsControl.Template> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <VirtualizingStackPanel IsItemsHost="True"> 
          </VirtualizingStackPanel> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
       </ListBox> 
Смежные вопросы