DataTemplate,样式,触发器

时间:2010-04-05 18:49:49

标签: wpf triggers datatemplate styles itemtemplate

我有一个<ListBox>,其中包含自定义<ListBox.ItemTemplate><DataTemplate>

 <ListBox>
      <ListBox.ItemTemplate>
           <DataTemplate>
               <Border BorderBrush="Black" BorderThickness="2" CornerRadius="5">
                    <Image Source="{Binding Picture}" />
                </Border>
           </DataTemplate>
      </ListBox.ItemTemplate>
</ListBox>

现在,当我选择ListBoxItem时,蓝色的行选择变得难看。我想改变它。我想只为边框的背景着色而没有别的。我还想改变MouseOver行为。我尝试了通过触发器,但ContentPresenter没有Background属性。

UPD:

好吧,我设法改变MouseEnterMouseLeave的背景:

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

但是当项目被选中时仍然无法更改Background。我正在努力:

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

不起作用

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

您可以使用触发器执行此操作。我在我的一个项目中有这样的事情:

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

虽然它不会更改边框颜色,但它会显示如何更改背景。所以,也许尝试将其设置为null。此触发器是自定义Style的一部分,其中使用IsMouseOver属性实现跟踪。

HTH