1

Imagine I have a data bound ListView and in the <ControlTemplate.Triggers>

I have the following

<DataTrigger Binding="{Binding Path=Status}" Value="Completed">
    <Setter Property="Background" Value="{StaticResource CompletedBackground}" />
    <Setter Property="Foreground" Value="Black" />
</DataTrigger>

I want that to be bound to a Style i have in my Grid.Resources which looks like the following:

<Style x:Key="CompletedBackground" TargetType="ListViewItem">
    <Setter>
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFBCFAA6" Offset="0"/>
                <GradientStop Color="#FFA3E88B" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
</Style>

However, as you might imagine this doesn't work, suprise suprise, you can't bind "Setter" to "Background", so my question is, how do I actually solve the problem?

I've looked through the following a lot of times, cant find any information here.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183

1 Answers1

2

What you're trying to do is fundamentally flawed. For starters, your style's setter doesn't specify a target property. Presumably, the target property should be Background:

<Style x:Key="CompletedBackground" TargetType="ListViewItem">
    <Setter Property="Background">
        <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFBCFAA6" Offset="0"/>
                        <GradientStop Color="#FFA3E88B" Offset="1"/>
                </LinearGradientBrush>
        </Setter.Value>
    </Setter>
</Style>

Secondly, you're then trying to assign a Style instance to the Background property, which is of type Brush, not Style.

Depending on exactly what you're trying to achieve, you should be able to just change the Style to a Brush resource:

<LinearGradientBrush x:Key="CompletedBackground" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FFBCFAA6" Offset="0"/>
    <GradientStop Color="#FFA3E88B" Offset="1"/>
</LinearGradientBrush>

Then use it from your trigger in the same fashion you already are.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • Fast answer Kent :). I can't do it faster than you :)). Also I think Filip is referencing style, which is defined inside control template's logical tree. Probably worth mention to define it inside ... Not in the grid. Cheers. – Anvaka Sep 28 '09 at 12:22
  • I want it to be globally accessable by all my ListViews, so not only in that controltemplate. Thanks – Filip Ekberg Sep 28 '09 at 12:28