.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > Style Trigger is not getting applied on ListviewItem in xp but works fine in Vista
Ask a questionAsk a question
 

AnswerStyle Trigger is not getting applied on ListviewItem in xp but works fine in Vista

  • Friday, October 30, 2009 8:27 AMVaibhav Nagar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi ,
    I have a wpf listview in which i am using datatemplates and cell templates. I have created triggers in the styles for IsSelected and IsMouseOver of Listview Item. I am  facing a wierd behaviour in xp the IsMouseOver trigger is working properly but IsSelected trigger is not working in xp but working in Vista.
    Here is the xaml which i am using:
    <ListView Grid.Row ="0" Margin="5" VirtualizingStackPanel.IsVirtualizing="True" IsSynchronizedWithCurrentItem="True"local:ListViewSorter.IsListviewSortable="True" x:Name='myListView' d:LayoutOverrides="GridBox" ForceCursor="False" >
    <ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
    <Setter Property="MinWidth" Value="150"/>
    <Setter Property="Height" Value="24"/>
    <Setter Property="Width" Value="Auto"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="Margin" Value="0,0,20,0"/>
    <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" Value="Blue"/>
    </Trigger>
    <Trigger Property="IsSelected" Value="True">
    <Setter Property="Background" Value="Orange" />
    </Trigger>
    </Style.Triggers>
    </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
    <GridView AllowsColumnReorder="False" ColumnHeaderContainerStyle="{DynamicResource CustomHeaderStyle}">
    <!-- This is the first column -->
    <GridViewColumn Header="MITOBJECT_NAME" Width="100">
    <GridViewColumn.CellTemplate >
    <DataTemplate >
    <Border BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" Padding="2,1,1,1" Width="Auto"
    eight
    ="Auto" BorderThickness="0,0,0,1" Margin="-7,-2,-12,0">
    <Grid Background="{x:Null}" Margin="0,0,10,0">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto"/>
    <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Image Grid.Column="0" Width="16" Height="16" Source="./tw.ico"/>
    <TextBlock Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,0,0,0" Text="{Binding Col3}" TextTrimming="CharacterEllipsis" ToolTipService.ToolTip ="{Binding Col3}"/>
    </Grid>
    </Border>
    </DataTemplate>
    </GridViewColumn.CellTemplate>
    </GridViewColumn >
    </GridView>
    <
    /ListView.View>
    </ListView >

Answers

  • Wednesday, November 04, 2009 9:18 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Vaibhav Nagar,
    -->IsMouseOver trigger is working properly but IsSelected trigger is not working in xp but working in Vista.

    Based on my experience, it was probably cuased by the theme defined in XP, i.e., there is a default IsMouseOver trigger in the theme. so one potential workaround is override the template of ListViewItem, you can just invoke XamlWriter.Save method to get the default control template of ListViewItem, and make a little bit modification in the XAML markup.

    Thanks.
    Sincerely.
    Jim Zhou -MSFT
  • Tuesday, November 10, 2009 8:20 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Vaibhav Nagar,
    It seems that you did not add the IsMouseOver and IsSelected property triggers in the ListViewItem template, please see the below code sample.
    There is another thing to note in this case, in the trigger section, it is not the ContentPresenter we want to change Background property, ContentPresenter class does not expose Background property, here, we need to change the background of the external Border control instead.
    Code snippet:

      <Window.Resources>

            <x:Array x:Key="data" Type="{x:Type s:String}">

                <s:String>Item1</s:String>

                <s:String>Item2</s:String>

                <s:String>Item3</s:String>

            </x:Array>

        </Window.Resources>

        <DockPanel>

            <ListView Name="ListViewTempDetails"

                      ItemsSource="{Binding Source={StaticResource data}}"

                      IsSynchronizedWithCurrentItem="True" 

                      ForceCursor="False"

                      Margin="0,3,3,123" BorderThickness="1,1,1,1"

                   >

                <ListView.ItemContainerStyle>

                    <Style TargetType="ListViewItem" x:Name="Lst">

                        <Setter Property="Template">

                            <Setter.Value>

                                <ControlTemplate TargetType="ListViewItem"

                                                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                                xmlns:s="clr-namespace:System;assembly=mscorlib">

                                    <Border  Padding="{TemplateBinding Control.Padding}"

                                             Name="Bd" SnapsToDevicePixels="True">

                                        <GridViewRowPresenter

                                            Content="{TemplateBinding ContentControl.Content}"

                                            Name="ContentSite"

                                     VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"

                                     SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"

                                     TextElement.FontSize="12">

                                            <GridViewRowPresenter.Columns>

                                                <TemplateBinding Property="GridView.ColumnCollection">

                                                </TemplateBinding>

                                            </GridViewRowPresenter.Columns>

                                        </GridViewRowPresenter>

                                    </Border>

                                    <ControlTemplate.Triggers>

                                        <Trigger Property="UIElement.IsEnabled" >

                                            <Setter  Property="TextElement.Background"

                                                     TargetName="ContentSite">

                                                <Setter.Value>

                                                    <SolidColorBrush>Black</SolidColorBrush>

                                                </Setter.Value>

                                            </Setter>

                                            <Trigger.Value>

                                                <s:Boolean>true</s:Boolean>

                                            </Trigger.Value>

                                        </Trigger>

                                        <Trigger Property="UIElement.IsMouseOver">

                                            <Setter  Property="Background"

                                                     TargetName="Bd">

                                                <Setter.Value>

                                                    <SolidColorBrush>Orange</SolidColorBrush>

                                                </Setter.Value>

                                            </Setter>

                                            <Trigger.Value>

                                                <s:Boolean>true</s:Boolean>

                                            </Trigger.Value>

                                        </Trigger>

                                        <Trigger Property="ListViewItem.IsSelected" >

                                            <Setter  Property="Background"

                                                     TargetName="Bd">

                                                <Setter.Value>

                                                    <SolidColorBrush>Red</SolidColorBrush>

                                                </Setter.Value>

                                            </Setter>

                                            <Trigger.Value>

                                                <s:Boolean>true</s:Boolean>

                                            </Trigger.Value>

                                        </Trigger>

                                    </ControlTemplate.Triggers>

                                </ControlTemplate>

                            </Setter.Value>

                        </Setter>

                    </Style>

                </ListView.ItemContainerStyle>

                <ListView.View>

                    <GridView ColumnHeaderContainerStyle="{DynamicResource CustomHeaderStyle}">

                        <GridViewColumn  Header="Name" Width="160">

                            <GridViewColumn.CellTemplate>

                                <DataTemplate>

                                    <Border

                                        BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}"

                                        Padding="2,1,1,1"  Width="Auto" Height="Auto" BorderThickness="0,0,0,1" Margin="-7,-2,-12,0">

                                        <Grid  Background="{x:Null}" Margin="0,0,10,0">

                                            <Grid.ColumnDefinitions>

                                                <ColumnDefinition Width="Auto"/>

                                                <ColumnDefinition/>

                                            </Grid.ColumnDefinitions>

                                            <TextBlock Grid.Column="1" HorizontalAlignment="Left"

                                                       VerticalAlignment="Center" Margin="5,0,0,0"

                                                       Text="{Binding Path=.}"/>

                                        </Grid>

                                    </Border>

                                </DataTemplate>

                            </GridViewColumn.CellTemplate>

                        </GridViewColumn>

                    </GridView>

                </ListView.View>

            </ListView>

        </DockPanel>

    If you are still having any issues with this, please feel free to ask.
    Thanks.


    Jim Zhou -MSFT

All Replies

  • Wednesday, November 04, 2009 9:18 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Vaibhav Nagar,
    -->IsMouseOver trigger is working properly but IsSelected trigger is not working in xp but working in Vista.

    Based on my experience, it was probably cuased by the theme defined in XP, i.e., there is a default IsMouseOver trigger in the theme. so one potential workaround is override the template of ListViewItem, you can just invoke XamlWriter.Save method to get the default control template of ListViewItem, and make a little bit modification in the XAML markup.

    Thanks.
    Sincerely.
    Jim Zhou -MSFT
  • Thursday, November 05, 2009 9:36 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Vaibhav Nagar,

    If you still have any additional issues with this, please feel free to ask.

    Thanks.
    Sincerely.


    Jim Zhou -MSFT
  • Thursday, November 05, 2009 11:59 AMVaibhav Nagar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Jim,
    Thanks  for the reply.
    Can you please provide me some sample code for the workaround that you have suggested?
  • Friday, November 06, 2009 9:05 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Vaibhav Nagar,

    Sure, you can check out the following code snippet:

    <
    ControlTemplate

       TargetType="ListViewItem"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:s="clr-namespace:System;assembly=mscorlib">

        <Border

          Padding="{TemplateBinding Control.Padding}"

          Name="Bd"

          SnapsToDevicePixels="True">

            <GridViewRowPresenter

             Content="{TemplateBinding ContentControl.Content}"

             Name="ContentSite"

             VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"

             SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"

             TextElement.FontSize="12"

             TextElement.Foreground="#FF4971A3">

                <GridViewRowPresenter.Columns>

                    <TemplateBinding>

                        <TemplateBinding

                      Property="GridView.ColumnCollection" />

                    </TemplateBinding>

                </GridViewRowPresenter.Columns>

            </GridViewRowPresenter>

        </Border>

        <ControlTemplate.Triggers>

           <!--set your own trigger here-->

            <Trigger

             Property="UIElement.IsEnabled">

                <Setter

                Property="TextElement.Foreground"

                TargetName="ContentSite">

                    <Setter.Value>

                        <SolidColorBrush>#FF888888</SolidColorBrush>

                    </Setter.Value>

                </Setter>

                <Trigger.Value>

                    <s:Boolean>False</s:Boolean>

                </Trigger.Value>

            </Trigger>

        </ControlTemplate.Triggers>

    </ControlTemplate>


    Thanks.
    Sincerely.


    Jim Zhou -MSFT
  • Monday, November 09, 2009 12:40 PMVaibhav Nagar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    HI Jim,
    I have tried the above code with my sample but still not working at my end.
    Here is my sample code, can you please check if iam doing some thing wrong?
     <ListView Name="ListViewTempDetails" ItemTemplate="{DynamicResource EmployeeTemplate}"   IsSynchronizedWithCurrentItem="True"  ForceCursor="False"  ItemsSource="{Binding}" 
                Margin="0,3,3,123" BorderThickness="1,1,1,1"
                    GridViewColumnHeader.Click="ListViewTempDetails_Click">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem" x:Name="Lst">
                            <Setter Property="Template">
                            <Setter.Value>
                              <ControlTemplate TargetType="ListViewItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                xmlns:s="clr-namespace:System;assembly=mscorlib">
                                <Border  Padding="{TemplateBinding Control.Padding}" Name="Bd" SnapsToDevicePixels="True">
                                  <GridViewRowPresenter  Content="{TemplateBinding ContentControl.Content}" Name="ContentSite"
                                     VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                                     SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
                                     TextElement.FontSize="12">
                                  <GridViewRowPresenter.Columns>
                                     <TemplateBinding Property="GridView.ColumnCollection">
                                     </TemplateBinding>
                                  </GridViewRowPresenter.Columns>
                              </GridViewRowPresenter>
                            </Border>
                                      <ControlTemplate.Triggers>
                                    <Trigger Property="UIElement.IsEnabled" >
                                    <Setter  Property="TextElement.Background" TargetName="ContentSite">
                                    <Setter.Value>
                                        <SolidColorBrush>Orange</SolidColorBrush>
                                            </Setter.Value>
                                        </Setter>
                                                <Trigger.Value><s:Boolean>true</s:Boolean></Trigger.Value>
                                            </Trigger>
                                       </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Setter.Value>
                              </Setter>
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.View>
                        <GridView ColumnHeaderContainerStyle="{DynamicResource CustomHeaderStyle}">
                            <GridViewColumn  Header="Name" Width="160">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate >
                                        <Border BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" Padding="2,1,1,1"  Width="Auto" Height="Auto" BorderThickness="0,0,0,1" Margin="-7,-2,-12,0">
                                            <Grid  Background="{x:Null}" Margin="0,0,10,0">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto"/>
                                                    <ColumnDefinition/>
                                                </Grid.ColumnDefinitions>
                                                <Image Grid.Column="0" Width="16" Height="16" />
                                                <TextBlock Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,0,0,0"  Text="{Binding Path= NAME}"/>
                                            </Grid>
                                        </Border>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
       </GridView>
                    </ListView.View>
                </ListView>

  • Tuesday, November 10, 2009 8:20 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Vaibhav Nagar,
    It seems that you did not add the IsMouseOver and IsSelected property triggers in the ListViewItem template, please see the below code sample.
    There is another thing to note in this case, in the trigger section, it is not the ContentPresenter we want to change Background property, ContentPresenter class does not expose Background property, here, we need to change the background of the external Border control instead.
    Code snippet:

      <Window.Resources>

            <x:Array x:Key="data" Type="{x:Type s:String}">

                <s:String>Item1</s:String>

                <s:String>Item2</s:String>

                <s:String>Item3</s:String>

            </x:Array>

        </Window.Resources>

        <DockPanel>

            <ListView Name="ListViewTempDetails"

                      ItemsSource="{Binding Source={StaticResource data}}"

                      IsSynchronizedWithCurrentItem="True" 

                      ForceCursor="False"

                      Margin="0,3,3,123" BorderThickness="1,1,1,1"

                   >

                <ListView.ItemContainerStyle>

                    <Style TargetType="ListViewItem" x:Name="Lst">

                        <Setter Property="Template">

                            <Setter.Value>

                                <ControlTemplate TargetType="ListViewItem"

                                                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                                xmlns:s="clr-namespace:System;assembly=mscorlib">

                                    <Border  Padding="{TemplateBinding Control.Padding}"

                                             Name="Bd" SnapsToDevicePixels="True">

                                        <GridViewRowPresenter

                                            Content="{TemplateBinding ContentControl.Content}"

                                            Name="ContentSite"

                                     VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"

                                     SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"

                                     TextElement.FontSize="12">

                                            <GridViewRowPresenter.Columns>

                                                <TemplateBinding Property="GridView.ColumnCollection">

                                                </TemplateBinding>

                                            </GridViewRowPresenter.Columns>

                                        </GridViewRowPresenter>

                                    </Border>

                                    <ControlTemplate.Triggers>

                                        <Trigger Property="UIElement.IsEnabled" >

                                            <Setter  Property="TextElement.Background"

                                                     TargetName="ContentSite">

                                                <Setter.Value>

                                                    <SolidColorBrush>Black</SolidColorBrush>

                                                </Setter.Value>

                                            </Setter>

                                            <Trigger.Value>

                                                <s:Boolean>true</s:Boolean>

                                            </Trigger.Value>

                                        </Trigger>

                                        <Trigger Property="UIElement.IsMouseOver">

                                            <Setter  Property="Background"

                                                     TargetName="Bd">

                                                <Setter.Value>

                                                    <SolidColorBrush>Orange</SolidColorBrush>

                                                </Setter.Value>

                                            </Setter>

                                            <Trigger.Value>

                                                <s:Boolean>true</s:Boolean>

                                            </Trigger.Value>

                                        </Trigger>

                                        <Trigger Property="ListViewItem.IsSelected" >

                                            <Setter  Property="Background"

                                                     TargetName="Bd">

                                                <Setter.Value>

                                                    <SolidColorBrush>Red</SolidColorBrush>

                                                </Setter.Value>

                                            </Setter>

                                            <Trigger.Value>

                                                <s:Boolean>true</s:Boolean>

                                            </Trigger.Value>

                                        </Trigger>

                                    </ControlTemplate.Triggers>

                                </ControlTemplate>

                            </Setter.Value>

                        </Setter>

                    </Style>

                </ListView.ItemContainerStyle>

                <ListView.View>

                    <GridView ColumnHeaderContainerStyle="{DynamicResource CustomHeaderStyle}">

                        <GridViewColumn  Header="Name" Width="160">

                            <GridViewColumn.CellTemplate>

                                <DataTemplate>

                                    <Border

                                        BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}"

                                        Padding="2,1,1,1"  Width="Auto" Height="Auto" BorderThickness="0,0,0,1" Margin="-7,-2,-12,0">

                                        <Grid  Background="{x:Null}" Margin="0,0,10,0">

                                            <Grid.ColumnDefinitions>

                                                <ColumnDefinition Width="Auto"/>

                                                <ColumnDefinition/>

                                            </Grid.ColumnDefinitions>

                                            <TextBlock Grid.Column="1" HorizontalAlignment="Left"

                                                       VerticalAlignment="Center" Margin="5,0,0,0"

                                                       Text="{Binding Path=.}"/>

                                        </Grid>

                                    </Border>

                                </DataTemplate>

                            </GridViewColumn.CellTemplate>

                        </GridViewColumn>

                    </GridView>

                </ListView.View>

            </ListView>

        </DockPanel>

    If you are still having any issues with this, please feel free to ask.
    Thanks.


    Jim Zhou -MSFT
  • Tuesday, November 10, 2009 9:43 AMVaibhav Nagar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Jim,
    It Worked finally thanks for  valuable inputs.
  • Wednesday, November 11, 2009 8:42 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Vaibhav Nagar,

    It's my pleasure, if you are still having issues with this, please feel free to ask.

    Thanks.
    Sincerely.
    Jim Zhou -MSFT