2

Trying to make a DataTemplate style, where it should change the colour of a couple of gradient stops based on the value of a boolean 'IsReported'. This is a MVVM Project.

However, when I add items to the ObservableCollection (in the view model) which the list box is binded to, it comes up with this error:

{"'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number '153' and line position '38'."}

Cannot find resource named 'timeSlotColorValue'. Resource names are case sensitive.

Here is the Style / Data Template:

       <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>

Here is the Model I'm basing my Data off:

    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; }
        }
    }

Can anyone shed some light on this, have I gone the wrong way about it? I've used this article for a guide: Change color of textbox with trigger, but doesn't seem to work in this scenario

4

2 回答 2

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>
于 2013-06-18T02:00:21.447 回答
0

StaticResource扩展将查找您指定为参数的资源x:Key。在您发布的代码中,您只指定了一个x:Name

于 2013-06-12T22:25:37.047 回答