-1

I have a requirement to display around 50 Labels that would change background color depending on the value of a view model boolean property. Each Label is associated with a different view model boolean property. How can I create a single style to do this that I can associate with all 50 labels, so that I don't have to declare a style for every label. Is there a way I can apply a SINGLE style and/or data trigger to ALL 50 labels, since each label will be bound to a different view model boolean property.

chuckp
  • 313
  • 1
  • 2
  • 15
  • possible duplicate http://stackoverflow.com/questions/3569974/wpf-global-style – Ayyappan Subramanian Apr 25 '17 at 14:21
  • Possible duplicate of [WPF - Global Style?](http://stackoverflow.com/questions/3569974/wpf-global-style) – Ayyappan Subramanian Apr 25 '17 at 14:22
  • I am well aware of how global styles work. My SPECIFIC question: is there a way I can apply a SINGLE style or data trigger to ALL 50 labels, since each label will be bound to a different view model boolean property. – chuckp Apr 25 '17 at 14:28
  • You don't need a Style. Just directly bind a Label's Background property to the appropriate view model property with a binding converter. Besides that, you may create an attached property that holds the source property path, and create an appropriate Binding in code behind. – Clemens Apr 25 '17 at 15:00
  • Thanks mucho!! Your answer will save me a lot of work. – chuckp Apr 25 '17 at 15:10

2 Answers2

0

I am well aware of how global styles work. My SPECIFIC question: is there a way I can apply a SINGLE style or data trigger to ALL 50 labels, since each label will be bound to a different view model boolean property.

No. You cannot dynamically change the binding of a DataTrigger unless you create it programmatically.

I am afraid there is no way to dynamically just replace the binding path of a DataTrigger and re-use the rest of the style or template in XAML.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks ... I was just wanting to make sure that there wasn't a way that I could do this before applying a different data trigger to 50 different labels. – chuckp Apr 25 '17 at 14:53
-2

I think you can try to create a custom Template for the Labels and add ControlTemplate trigger to change the background of label.

<Style x:Key="test" TargetType="Label">
            <Setter Property="BorderThickness" Value="1"></Setter>
            <Setter Property="BorderBrush" Value="Black"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBlock Text="test"></TextBlock>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding ColorValue}" Value="12">
                                <Setter Property="Background" Value="Aquamarine"></Setter>
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Above code is just to show the usage, it is not the exact answer.

  • 1
    I think this would work if all 50 labels were bound to the same view model boolean property .. HOWEVER ... each label is bound to its own separate boolean property. – chuckp Apr 25 '17 at 14:50
  • Totally agree with your point, Howeverm I think if each viewmodel has same property name this should work. Let me try it. I guess if we can have a base viewmodel with the property and make sure each viewmodel uses that property for that label. let me know if you think otherwise. – Karmjit Singh Apr 25 '17 at 16:10