尝试制作DataTemplate
样式,它应该根据布尔值'IsReported'的值更改几个渐变色标的颜色。这是一个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; }
}
}
任何人都可以对此有所了解,我是否走错了路? 我用这篇文章作为指南:Change color of textbox with trigger, 但似乎在这种情况下不起作用
答案 0 :(得分:0)
StaticResource
扩展名将查找您指定为参数的x:Key
的资源。在您发布的代码中,您只指定了x:Name
答案 1 :(得分:0)
我完全重新考虑了我的方法并通过使用单独的边框样式来解决它
<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>