¿Cómo me vinculo tanto a un enlace de datos como a un valor de propiedad en un disparador de estilo?

I want to grey out some text when some piece of data is "Ignored", but I don't want the grey out to happen when the item is selected. (Specifically, in high contrast mode, setting the color to the grey value causes the text to be unreadable)

This was my first attempt to do that.

<!-- .... -->
<Setter Property="Control.Foreground"
        Value="{DynamicResource {x:Static SystemColors.ControlTextColor}}" />
            <!-- Set gray text when not selected, and ignored. -->
            <Condition Property="ListBoxItem.IsSelected" Value="false" />
            <Condition Binding="{Binding Ignored}" Value="true" />
        <Setter Property="Control.Foreground"
                Value="{DynamicResource {x:Static SystemColors.GrayTextColor}}" />

Esto falla en tiempo de ejecución porque MultiDataTrigger needs Binding to be set on its conditions. (At least, I think that's why it is failing.)

¿Cómo puedo solucionar este problema?

preguntado el 09 de septiembre de 13 a las 22:09

2 Respuestas

Depending on where exactly you are using the Style, you can convert the first Condition to bind to the ListBoxItem.IsSelected property by using a RelativeSource binding.

Respondido el 09 de Septiembre de 13 a las 22:09

This is being applied to <ListBox.ItemContainerStyle>. - Billy ONeal

try this inside your first Condition: Binding = {Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}} - Andrés

Also - you should capitalize 'True' and 'False' inside your conditions - it's standard in XAML, even though it's not standard for xml. There are cases where it makes a difference. - Andrés

Upvoted Andrew's answer, I think you want to be binding to the IsSelected propiedad de la ListBoxItem, utilizando RelativeSource, although I'd try it this way:

<Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=Self}}" Value="false" />

That worked for me, using a Style en un parche de ResourceDictionary, and using it in a ListBox estableciendo el ItemContainerStyle propiedad.

Respondido el 09 de Septiembre de 13 a las 22:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.