¿Cómo me vinculo tanto a un enlace de datos como a un valor de propiedad en un disparador de estilo?
Frecuentes
Visto 77 veces
0
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.
<Style>
<!-- .... -->
<Setter Property="Control.Foreground"
Value="{DynamicResource {x:Static SystemColors.ControlTextColor}}" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<!-- Set gray text when not selected, and ignored. -->
<Condition Property="ListBoxItem.IsSelected" Value="false" />
<Condition Binding="{Binding Ignored}" Value="true" />
</MultiDataTrigger.Conditions>
<Setter Property="Control.Foreground"
Value="{DynamicResource {x:Static SystemColors.GrayTextColor}}" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
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?
2 Respuestas
3
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
2
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 wpf xaml data-binding or haz tu propia pregunta.
This is being applied to
<ListBox.ItemContainerStyle>
. - Billy ONealtry 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