= 10 then make th-6ren">
gpt4 book ai didi

c# - Setter 目标名称无法识别

转载 作者:行者123 更新时间:2023-12-02 17:40:39 27 4
gpt4 key购买 nike

我是 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.

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>

那么问题出在哪里呢?标签定义在更高的几行。

最佳答案

模板有自己的作用域,无法从外部访问。无论如何,您不能在样式触发器中使用 TargetName,如果您需要更改标签,请向其样式添加触发器。

关于c# - Setter 目标名称无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38149876/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com