views:

295

answers:

1

So I have a TreeViewItem that has the following style:

                   <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="HeaderTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Image Name="img" Width="20" Height="16" Stretch="Uniform" Source="Images/Folder.png"/>
                                        <TextBlock Text="{Binding}" Margin="5,0" />
                                    </StackPanel>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

When selected, the TextBlock AND Image are highlighted. I'm trying to just highlight the TextBlock so it functions like the folders tree in file explorer.

A: 

You will need to roll your own highlight mark up, so instead of letting the control painting the entire panel background blue, you set your own highlight formatting based on a trigger when TreeViewItem.IsSelected is True.

In your case this would be setting the text container background to blue (when set, white normally) and leaving the image container background as white, whilst setting the overall container background to white.

The method is described here: link text

Guy
That makes sense, but it's not quite as easy as in the example you provided. Each time I try to use TargetName to address the TextBlock I get an error saying it cannot be accessed.
AKoran
http://stackoverflow.com/questions/248545/wpf-trigger-for-isselected-in-a-datatemplate-for-listbox-items
Guy
Don't take this the wrong way, but it doesn't help a newbie to WPF much when you point to an example that just kinda is close to what I'm trying to do. No matter what I try, every time I refer back to the TextBlock I get a "TargetName property cannot be set on a Style Setter" error.
AKoran
I think the reason you are getting that error is because a style setter will always set the property on the control which uses that style, which can be redirected by a TargetName property. If you look in the first example I posted, the trigger is not part of a style, its just part of the control template, and I think that may be where you are going wrong.
Guy