我是 WPF 的初学者,我尝试使用 DataTrigger 编写 WPF 部分。
这里需要逻辑:
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.
但他找不到我的目标名称为“StatusColor”的标签。
找到下面的代码:
<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>
所以有什么问题?标签定义在更高的几行。
该模板有自己的范围,无法从外部访问。无论如何,您不能在样式触发器中使用 TargetName,如果您需要更改标签,请向其样式添加触发器。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)