Search code examples
wpfxamlhyperlinkdatatemplatemultidatatrigger

Hyperlink Foreground styling in a listview not happening


I have a problem that is driving me mad, and I just dont seem to be able to find a solution.

I have a Listview, for which the listivew ItemTemplate is a Datatemplate that looks like this:

<DataTemplate x:Key="template" >
  <TextBlock  Foreground="Green"  >
    <Hyperlink   Command="{Binding LoadReportCommand}"
              CommandParameter="{Binding DisplayName}"  >
            <TextBlock  Text="{Binding DisplayName}"  />
    </Hyperlink>       
   </TextBlock>
</DataTemplate>

The style for the hyperlink is as follows:

<Style x:Key="{x:Type Hyperlink}" TargetType="{x:Type Hyperlink}">
    <Setter Property="Foreground" Value="Green" />
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding RelativeSource={RelativeSource   FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" Value="True" />
            <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=Selector.IsSelectionActive}" Value="True" />
            </MultiDataTrigger.Conditions>
            <Setter Property="Foreground" Value="White"/>
        </MultiDataTrigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
        </Trigger>
    </Style.Triggers>
</Style>

When I use it as is it does not turn the foreground white as would be expected when the item is selected. It does turn the foreground gray if the command is not enabled as is expected. If I remove the condition for Selector.IsSelectionActive it does turn the foreground white when the item is selection, but it is also white if the item is selected but not active.... I want the Inactive selection to be green. I have tried this also with just a plane Textblock, a button with a very basic style... nothing seems to work.

The listview is in a usercontrol that is used in another user control. The Datatemplate is in the resourcedictionary of the second user control, and bound to a dependency property of the first user control of type DataTemplate, and the style for the hyperlink is in a separate Resourcedictionary.

Any Help will be much appreciated


Solution

  • Hmm Selector.IsSelectionActive is that false when the control hasn't got focus? If so maybe you could use an ancestorbinding on the listview and check for focus?

    Now that I look at it - you are binding to an attached property - there is a slight changed syntax for that - try Path=(Selector.IsSelectionActive)} instead.

    Green links - odd :)