0
votes

I got a grid with a label, a button and a Textbox in it. I want to set the label forground to red & the button property "IsEnabled" To False if the value of the label.Content = "Invalid". However I am not able to set the setter to the targetname in this datatrigger because "Targetame property cannot be set on a Style Setter." How can I fix this so I can use setters for multiple elements with just 1 trigger, I know I can go and put a exact the same datatrigger in the Button.Styletriggers but perhaps there is a better way to do tihs?

   <Button Content="Save" Click="btnsave_Click" Grid.Column="1" Grid.Row="2" x:Name="btnsave" IsEnabled="True" />
            <Label Content="{Binding ElementName=txtclc, Path=Text, Converter={StaticResource convertcode}}" Name="lblcheckclc"  Grid.Row="1" Grid.Column="2">
                <Label.Resources>
                    <Style TargetType="{x:Type Label}">
                        <Setter Property="Foreground" Value="Green" />

                        <Style.Triggers>
                            <DataTrigger  Binding="{Binding ElementName=lblcheckclc, Path=Content}" Value="Invalid">
                                <Setter Property="Foreground" Value="Red" />
                                <Setter TargetName="btnsave" Property="IsEnabled" Value="False"  /> <!--This line is the problem-->

                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Label.Resources>
            </Label>

PS: If I comment the 2nd setter then it works, (if the value of the label = "Invalid" then the forground becomes red

1
Any particular reason why you want to use an implicit style here? Don't you mean <Label.Style> instead?Louis Kottmann
@Baboon I dunno but isn t it the same?Maximc
It is not the same. Implicit styles are meant for global setters for a particular FrameworkElement. Right now, I believe you're only applying that style to the children of your Label, not to the label itself. I could be wrong though.Louis Kottmann

1 Answers

0
votes

Please take a look at this msdn forum for an answer to your question

Also your Triggers section is incorect, you should use:

<Style.Triggers>
   <Trigger Property="Content" Value="Invalid">
      <Setter Property="Foreground" Value="Red" />
   </Trigger>
</Style.Triggers>

Use a trigger when you plan to monitor a property of your element like Content.

To make your example work either:

  • Bind your button's IsEnabled property to your label's content and implement a converter that converts string to bool
  • Create a style for your button with a data trigger that sets the IsEnabled property