I'm a beginner at WPF and I try to write a WPF part with DataTrigger.
Here the needed logic:
If the variable "iBottleCount" >= 10 then make the background of a label green. If the variable "iBottleCount" < 10 then make the background of a label yellow. If the variable "iBottleCount" = 0 then make the background of a label red.
But he can'f find my label with the targed name "StatusColor".
Find the code below:
<DataGridTemplateColumn Header="Status" Width="80" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="StatusText" Height="15" HorizontalAlignment="Left" Margin="2,2,2,2" Text="" VerticalAlignment="Top" Grid.Row="0" Grid.Column="1"/>
<Label x:Name="StatusColor" Content="" Background="green" HorizontalAlignment="Center" Margin="5,5,5,5" VerticalAlignment="Top" Height="10" Width="20" Grid.Row="0" Grid.Column="0"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding iBottleCount}" Value="=>10">
<!-- PROBLEM IS IN THIS LINE -->
<Setter TargetName="StatusColor" Property="Background" Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
So what's the problem? The label is defined some lines higher.
The template has its own scope that cannot be accessed from outside. You cannot use TargetName in Style triggers anyway, if you need to change the Label, add a trigger to its Style.
Without a good Minimal, Complete, and Verifiable code example that shows clearly what you're trying to do, it's impossible to know for sure what the best fix is. That said…
First off, the comment in H.B.'s answer is correct: you're trying to access a named element where that name is not in scope at the point where you are trying to access it, and even if you could, the Style trigger can't use the TargetName property.
Certainly one option is to move the trigger to the template. This, however, will require that the template be tied to the view model you're using. This might actually be okay in your case. There's not enough context in the question to know.
If you'd rather stay closer to the design you've got now, where the trigger is in the DataGridCell style, you can use TemplateBinding to accomplish what you want. For example…
In the template:
<Label Content="" Background="{TemplateBinding Background}" HorizontalAlignment="Center"
Margin="5,5,5,5" VerticalAlignment="Top" Height="10" Width="20"
Grid.Row="0" Grid.Column="0"/>
And then, setting the Background property of the DataGridCell will propagate down into the template. I.e. in your style's trigger:
<Setter Property="Background" Value="Green"/>
I.e. just remove the TargetName attribute from your existing code.
Obviously, this works only if you have a single Background value you want to set in the template. You'll need something more elaborate if you have a more complex scenario. Also, of course don't forget that if you want to provide a default value for the Background property, you need to include a non-trigger <Setter .../> element in the style to set that default value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With