数据模板、样式、触发器

发布于 2024-08-27 22:48:31 字数 1399 浏览 2 评论 0原文

我有一个 ,其中包含自定义

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

不起作用

I have a <ListBox> with custom <ListBox.ItemTemplate> and <DataTemplate> in it:

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

Now when I'm choosing the ListBoxItem it gets ugly with blue colored row selection. I'd like to change it. I want to color only border's background and nothing else. Also I want to change MouseOver behavior. I've tried trough triggers, but ContentPresenter doesn't have Background property.

UPD:

Well, I've managed to change the background on MouseEnter and MouseLeave:

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

But still can't change the Background when item's selected. I'm trying through:

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

Doesn't work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说好的呢 2024-09-03 22:48:31

您要查找的颜色位于 ListBoxItem 模板内的两个触发器中,而不是 ItemTemplate 中。要更改此设置,您需要编辑列表框的 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>

The coloring you're looking for is in two Triggers inside the template for ListBoxItem, not the ItemTemplate. To change this you need to edit the ItemContainerStyle for the ListBox. This is the default that can be used as a starting point:

    <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>
空‖城人不在 2024-09-03 22:48:31

您可以使用触发器来做到这一点。我在我的一个项目中有这样的内容:

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

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

华泰

You can do this by using triggers. I have in one of my projects something like this:

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

Though it doesn't change Border color, it shows how to change the Background. So, maybe try to set this to null. This triggers are part of a custom Style in which a tracking is achieved using IsMouseOver property.

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文