当数据绑定到梯度停止时,找不到可视树元素
本文关键字:找不到 可视 元素 数据绑定 | 更新日期: 2023-09-27 18:18:01
尝试制作DataTemplate
风格,它应该根据布尔值' isreporting '改变一对渐变停止的颜色。这是一个MVVM项目。
然而,当我向列表框绑定到的ObservableCollection(在视图模型中)添加项时,它出现了这个错误:
{"'为'System.Windows.Markup '提供值。StaticResourceHolder'抛出异常。'行号为153,行位为38。"}
无法找到名为'timeSlotColorValue'的资源。资源名区分大小写
样式/数据模板:
<LinearGradientBrush x:Key="bordBackground" EndPoint="1,1" StartPoint="0,0">
<GradientStop x:Name="timeSlotColorValue" Color="Lime" Offset="0" />
<GradientStop Color="Transparent" Offset="0.2"/>
<GradientStop Color="Transparent" Offset="0.8"/>
<GradientStop x:Name="timeSlotColorValue2" Color="Lime" Offset="1"/>
</LinearGradientBrush>
<Style TargetType="ListBox" x:Key="timeSlotTemplate">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate DataType="model:DVRTimeSlot">
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsReported}" Value="true">
<Setter Property="GradientStop.Color" TargetName="{StaticResource timeSlotColorValue}" Value="Red" />
<Setter Property="GradientStop.Color" TargetName="{StaticResource timeSlotColorValue2}" Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding TimeString}" Padding="2" FontSize="13" FontWeight="Black" >
<TextBlock.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0,1">
<GradientStop Color="LightGray" Offset="0"/>
<GradientStop Color="Azure" Offset="1"/>
</LinearGradientBrush>
</TextBlock.Background>
</TextBlock>
<Border Grid.Column="1" Background="{StaticResource bordBackground}">
<Border.Resources>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Right" />
<Setter Property="Padding" Value="5,0"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="FontWeight" Value="SemiBold" />
</Style>
<Style TargetType="Border">
<Setter Property="Padding" Value="3" />
<Setter Property="Margin" Value="-3,0,-3,0" />
<Setter Property="MinWidth" Value="240"/>
</Style>
</Border.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Reason}" TextAlignment="Center"/>
</Grid>
</Border>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
这是我基于数据的模型:
public class DVRTimeSlot
{
public DVRTimeSlot()
{
}
public bool IsReported { get; set; }
public string Reason { get; set; }
public CallReportCategory Category { get; set; }
public DateTime TimeValue { get; set; }
public string TimeString
{
get { return TimeValue.Hour + ":" + TimeValue.Minute; }
}
}
有谁能解释一下吗,我走错路了吗?我用这篇文章作为指导:用触发器改变文本框的颜色,但在这种情况下似乎不起作用
StaticResource
扩展将查找您指定为参数的x:Key
资源。在您发布的代码中,您只指定了x:Name
我完全重新考虑了我的方法,并通过为边框使用单独的样式来解决这个问题
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsReported}" Value="True">
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="Red" Offset="0"/>
<GradientStop Color="Transparent" Offset="0.2"/>
<GradientStop Color="Transparent" Offset="0.8"/>
<GradientStop Color="Red" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsReported}" Value="False">
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
<GradientStop Color="Lime" Offset="0"/>
<GradientStop Color="Transparent" Offset="0.2"/>
<GradientStop Color="Transparent" Offset="0.8"/>
<GradientStop Color="Lime" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
<Setter Property="Padding" Value="3" />
<Setter Property="Margin" Value="-3,0,-3,0" />
<Setter Property="MinWidth" Value="240"/>
</Style>
</Grid.Resources>