简体   繁体   中英

WPF ListBoxItem Style Is Overriding My ItemTemplate?

I have a listbox that is bound to a list Items of objects that have two properties: Name and IP .

I would like to set a tooltip for each item that displays the IP address on hover, so I've done this:

    <ListBox ItemsSource="{Binding Items}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" ToolTip="{Binding IP}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

This works. However, when I apply the following style, it seems that my <DataTemplate> is being ignored completely and the list is just calling ToString() on my objects and the tooltip never shows up at all.

<Style TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd"
                 Background="{TemplateBinding Background}"
                 BorderBrush="{TemplateBinding BorderBrush}"
                 BorderThickness="{TemplateBinding BorderThickness}"
                 Padding="{TemplateBinding Padding}"
                 SnapsToDevicePixels="true">
                        <Grid>
                           <GridViewRowPresenter x:Name="gridrowPresenter"
                        Content="{TemplateBinding Property=ContentControl.Content}" />
                            <ContentPresenter x:Name="contentPresenter"
                        Content="{TemplateBinding Property=ContentControl.Content}"  Visibility="Collapsed" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="GridView.ColumnCollection" Value="{x:Null}">
                            <Setter TargetName="contentPresenter" Property="Visibility" Value="Visible"/>
                        </Trigger>
                        <Trigger Property="Control.IsMouseOver"  Value="true">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <SolidColorBrush Color="#FFE6E6E6" Opacity="0.75"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsSelected"  Value="true">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <SolidColorBrush Color="#FFA5C5D1" Opacity="0.75"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Foreground" Value="Black"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Why is this style preventing the DataTemplate from being applied?

Have you tried not binding the ContentPresenter's Content ? It should work without that by default. Binding it (or using a TemplateBinding) is probably disrupting the default ContentControl functionality.

Check the ListBoxItem default Template: msdn.microsoft.com/es-es/library/ms750821(v=vs.85).aspx

The ContentPresenter is left as-is, and it is the ContentControl logic what makes the rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM