0

This is a link for a question asked before, which concerns a TreeView:

WPF TreeView: How to style selected items with rounded corners like in Explorer

My question is : How to migrate this Solution on a ListView ?

The answers are a little disordered, so I didn't understand what's happening there !

Community
  • 1
  • 1
NTinkicht
  • 972
  • 2
  • 12
  • 34

2 Answers2

3

I thought that question sounded familiar. :)

So, you should just be able to use the same code, but then use Visual Studio to Find and Replace TreeView to ListView. Of course there are a few parts like the Image.Source and the IsMouseDirectlyOverItem helper that you may need to alter more carefully. For the most part, the Find and Replace function should work. TreeView and ListView have absolutely loads of identical properties.

Let me know if you have any problems doing this.

You can change your HierarchicalDataTemplate to a normal one like this:

<DataTemplate DataType="{x:Type viewmodels:ObjectBaseViewModel}">
    <StackPanel Orientation="Horizontal" Margin="2,1,5,2">
        <Grid Margin="0,0,3,0">
            <Image Name="icon" Source="/ExplorerTreeView/Images/folder.png"/>
        </Grid>
        <TextBlock Text="{Binding Name}" />
    </StackPanel>
</DataTemplate>

Let me know if I have misunderstood your problem.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Always here for save others :) thanks @Sheridan, I did that in the first time, but I had a problem, where to put my 'ListView.ItemTemplate' ??? I tried to put it Instead of 'HierarchicalDataTemplate' but VS said that I can't put this in a 'ListView.Ressources' – NTinkicht Oct 07 '13 at 15:21
  • @Sherdian ! I have a little problem: when I select an `item`, and after that select another, the `Item` selected before doesn't come back to its "not selected state". I think it's because I removed the Condition ` Property="IsSelectionActive" `. VS didn't allow me to put it for a ListViewItem ! how to do ? – NTinkicht Oct 08 '13 at 12:41
  • Sorry @Alvaro, I've just run out of time here again... if you ask this in a new question, someone might be able to help you and I'll check when I return later to see if you have an answer. – Sheridan Oct 08 '13 at 12:45
  • Did you ask a new question? I could find one. – Sheridan Oct 08 '13 at 13:49
  • No I didn't, I just answered by the solution I found. – NTinkicht Oct 08 '13 at 14:20
0

Well, I make some changes and I think now it works Better:

<ListView ...>
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="BorderThickness" Value="1"/>
                <Style.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="helpers:ListViewHelper.IsMouseDirectlyOverItem" Value="False"/>
                            <Condition Property="IsSelected" Value="False"/>
                            <Condition Property="IsFocused" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="Transparent"/>
                        <Setter Property="BorderBrush" Value="Transparent"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="helpers:ListViewHelper.IsMouseDirectlyOverItem" Value="True"/>
                            <Condition Property="IsSelected" Value="False"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                    <GradientStop Color="#FFF8F8F8" Offset="0"/>
                                    <GradientStop Color="#FFE5E5E5" Offset="1"/>
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="BorderBrush" Value="#D9D9D9"/>
                    </MultiTrigger>

                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsFocused" Value="True"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                    <GradientStop Color="#FFFAFBFD" Offset="0"/>
                                    <GradientStop Color="#B8D6FB " Offset="1"/>
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="BorderBrush" Value="#D9D9D9"/>
                    </MultiTrigger>
                </Style.Triggers>
                <Style.Resources>
                    <Style TargetType="Border">
                        <Setter Property="CornerRadius" Value="2"/>
                    </Style>
                </Style.Resources>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemTemplate>
            <DataTemplate>
             ...   
            </DataTemplate>

        </ListView.ItemTemplate>
        <ListView.Resources>

            <!-- Brushes for the selected item -->
            <LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FFFAFBFD" Offset="0"/>
                <GradientStop Color="#B8D6FB " Offset="1"/>
            </LinearGradientBrush>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
        </ListView.Resources>
    </ListView>

The problem I had with the last answer, is that: IF for example the parent control of this ListView is also a ListView (or a TreeView) when you switch clicks from a listView to another, the last selected ListView always stay selected too... I think because it's independant from the other children.

NTinkicht
  • 972
  • 2
  • 12
  • 34
  • Alvaro, while I applaud the fact that you have returned to this question, to provide a better solution for other users, I am not convinced that you should accept this as the correct answer. I personally don't mind because I have plenty of reputation points, but I feel that others could object. When you ask a question, users take some time and effort to answer it. I don't think that it is right that you then add your own answer which implements some new requirements that are not even mentioned in your question. If I were you, I'd have added this as a solution summary update to my question. – Sheridan Oct 31 '13 at 10:23
  • @Sheridan ! It's not about Reputation !! do you know that when you change acceptance you have -2 reputations !!! I did that because I thought that it was more adapted for any other user who will have a problem similar of this ! that's all !, I don't care about any reputation here ! I think that the interest of the users is more important. – NTinkicht Oct 31 '13 at 10:31
  • I'll give it back to you because I think you have more experience than me in WPF, so if you see that yours is better, No problem bro :) – NTinkicht Oct 31 '13 at 10:33
  • it's not at all that I think that my answer is better than yours... it's more that you have now answered a question, or implemented requirements that you didn't ask or mention in your question. I *did* say that I didn't mind losing the reputation and I'm happy for you to take it back. I only mentioned this because I did a similar thing as a junior member and that somehow outraged a number of users... so my comment was just meant as a polite warning for the future. Hopefully, I have not offended you. – Sheridan Oct 31 '13 at 10:58
  • @Sheridan, thank you ! no worries. I really respect you because you are always here to help others. – NTinkicht Oct 31 '13 at 13:15