2010-04-05 4 views
0

나는 그것을에서 사용자 지정 <ListBox.ItemTemplate><ListBox><DataTemplate> 있습니다. 나는 그것을 바꾸고 싶다. 국경의 배경 만 색칠하고 다른 것은 색칠하지 않습니다. 또한 MouseOver 동작을 변경하고 싶습니다. 트로프 트리거를 시도했지만 ContentPresenter에는 Background 속성이 없습니다.DataTemplate을, 스타일, 트리거

UPD : 항목의 선택 때 Background 변경할 수 없습니다 여전히

<EventTrigger RoutedEvent="Border.MouseEnter"> 
     <BeginStoryboard> 
       <Storyboard > 
       <ColorAnimation Storyboard.TargetProperty="Background.Color" 
        To="LightBlue" Duration="0:0:0.03"/> 
       </Storyboard> 
     </BeginStoryboard> 
    </EventTrigger> 

그러나 :

글쎄, 나는 MouseEnterMouseLeave의 배경을 변경 관리했습니다. 나는 통해 노력하고있어 :

<Trigger Property="ListBoxItem.IsSelected" Value="True"> 
     <Setter Property="Background" Value="Red" /> 
    </Trigger> 

답변

2

착색 당신이 찾고있는이 ListBoxItem의 아닌 ItemTemplate을위한 템플릿 내부에 두 개의 트리거입니다. 이를 변경하려면 ListBox에 대한 ItemContainerStyle을 편집해야합니다. 이것은 시작 지점으로 사용할 수 있습니다 기본값입니다

<ListBox> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="Background" Value="Transparent"/> 
       <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
       <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
       <Setter Property="Padding" Value="2,0,0,0"/> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
          <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> 
           <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
          </Border> 
          <ControlTemplate.Triggers> 
           <Trigger Property="IsSelected" Value="true"> 
            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
           </Trigger> 
           <MultiTrigger> 
            <MultiTrigger.Conditions> 
             <Condition Property="IsSelected" Value="true"/> 
             <Condition Property="Selector.IsSelectionActive" Value="false"/> 
            </MultiTrigger.Conditions> 
            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
           </MultiTrigger> 
           <Trigger Property="IsEnabled" Value="false"> 
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 
0

당신은 트리거를 사용하여이 작업을 수행 할 수 있습니다 작동하지 않습니다. 나는 내 프로젝트 중 하나에 다음과 같은 것을 가지고있다.

<Trigger Property="IsSelected" Value="true"> 
    <Setter Property="Panel.ZIndex" Value="1"/> 
    <Setter Property="Background" TargetName="Bd" Value="{StaticResource TabItemSelectedBackground}"/> 
</Trigger> 

테두리 색이 바뀌지 않지만 배경을 변경하는 방법이 나와있다. 그래서 이것을 null로 설정하십시오. 이 트리거는 IsMouseOver 속성을 사용하여 추적을 수행하는 사용자 지정 스타일의 일부입니다.

HTH

+0

그래 잘 말한다 : 이 유형에 템플릿 속성 '에 isSelected'를 찾을 수 없습니다 'System.Windows.Controls.ContentPresenter' – Agzam

관련 문제