Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF VB.NET Data-Binding
Hi all,
I am back again... Smile | :) Frown | :( i am a lone tech person working in a team and have no one to help apart from you guys and Mr. Google. i am learning WPF now and created an application. it is working fine in view and code separated.
It extracts details from a web site. list out all the details of the product and their specifications and dump it all in an excel. so far all is good.
now i want to databind the view of giving the look and the progress to the user.
i am able to get the values out from the class and displaying in the lable. i want to show how much of each web is loaded by changing the color of the lable.
i want to show the state "Loading" "Interactive" ETC. to the user with change in color in the same lable.
i am able to get the string to the lable but the color change is not happening or triggering.
MY cut down XAML code:
start of XAML:(thought this might be important)
 
<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>
    
 

 
part of the code where binding is happening
<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>
 </StackPanel>
 
my vb.net code to convert the color
 
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
End Namespace
 
can you please correct me
Posted 27-Feb-13 20:03pm
Edited 27-Feb-13 20:12pm
v2
Comments
Manu_vish at 28-Feb-13 4:56am
   
I ma able to do this with text-block. not sure why not in labels. any one have any idea??
Matej Hlatky at 28-Feb-13 4:58am
   
What is “Caption” property at the Label.Background binding – why no “Content”, or better VM’s “prdFrstState”? Why are you binding to Self?
Did you tried to place break point into your ColorConvert. Convert method and check provided value?
Also UpdateSourceTrigger=PropertyChanged is useless in Label, because it only displays data.
Manu_vish at 28-Feb-13 5:27am
   
Sorry.because of confusion I typed caption. It is actually content. as mentioned I change value of labels to loading, opening. Etc. So wanted to change colors as per text. Ya. I placed breaks. But it never hit that sub. I used snoop now and came to know that there is eror in getting values from content

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100