-1

我现在正在学习 WPF 并创建了一个应用程序。它在视图和代码分离中工作正常。它从网站中提取详细信息。列出产品的所有细节及其规格,并将其全部转储到 Excel 中。到目前为止一切都很好。现在我想为用户提供外观和进度的数据绑定视图。我能够从类中获取值并显示在标签中。我想通过更改标签的颜色来显示每个网页的加载量。这是 XAML

<Window x:Class="WebExtractor.AvnetMainClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Avenet_Web_Extractor.WebExtractor"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Title="Avnet Product Details Extractor" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="300" Width="538" 
ResizeMode="NoResize">

<Window.Resources>
    <local:ColorConvert x:Key="colorConvert"/>

</Window.Resources>

数据绑定在这里

<StackPanel Margin="164,4,4,5" HorizontalAlignment="Stretch" >
                    <Label Name="lbl1PrdFll" BorderBrush="Black" BorderThickness="1" Width="312" Height="27" Margin="0,2,0,0" Background="{Binding RelativeSource={RelativeSource Self}, Path=Caption,Converter={StaticResource colorConvert}}" Content="{Binding Path=prdFrstState, UpdateSourceTrigger=PropertyChanged}" ></Label>
                    <Label Name="lbl2PrdFll" BorderBrush="Black" BorderThickness="1" Width="312" Height="27" Margin="0,2,0,0" Background="{Binding RelativeSource={RelativeSource Self}, Path=Caption,Converter={StaticResource colorConvert}}"  Content="{Binding Path=prdScndState, UpdateSourceTrigger=PropertyChanged}"></Label>
                    <Label Name="lbl3PrdFll" BorderBrush="Black" BorderThickness="1" Width="312" Height="27" Margin="0,2,0,0" Background="{Binding RelativeSource={RelativeSource Self}, Path=Caption,Converter={StaticResource colorConvert}}"  Content="{Binding Path=prdLstState, UpdateSourceTrigger=PropertyChanged}"></Label>

用于颜色更改的 vb.net 代码是:

Namespace WebExtractor

<ValueConversion(GetType(String), GetType(SolidColorBrush))> _
Public Class ColorConvert
    Implements IValueConverter
    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
        Dim boundWord As String = TryCast(value, String)
        Dim returnBrush As SolidColorBrush = Nothing
        Select Case boundWord.ToLower().Trim()

            Case "UnInti"
                returnBrush = New SolidColorBrush(Colors.Red)
            Case "Loading"
                returnBrush = New SolidColorBrush(Colors.Red)
            Case "Loaded"
                returnBrush = New SolidColorBrush(Colors.LightPink)
            Case "Interactive"
                returnBrush = New SolidColorBrush(Colors.Pink)
            Case "Complete"
                returnBrush = New SolidColorBrush(Colors.Green)
        End Select

        Return returnBrush
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New Exception("Cant convert back")
    End Function
End Class

结束命名空间

是否有任何选项可以像“lblprdfrst.background=color.red”一样即时更改颜色

4

1 回答 1

0

您可以按照 makc 的建议将背景属性直接绑定到视图模型上的属性,或者如果您根据已知的固定规则更改颜色,则可以使用数据触发器执行此操作,该触发器基于您的 boundword 变量的值. 这是一个包含数据触发器示例的教程站点http://zamjad.wordpress.com/2009/08/21/data-trigger-in-wpf/

于 2013-02-28T11:30:19.143 回答