-1

私は今WPFを学んでおり、アプリケーションを作成しています。ビューとコードが分離されて正常に動作しています。Web サイトから詳細を抽出します。製品のすべての詳細とその仕様をリストアップし、すべてを 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 に答える