none
Creating a IValueConverter

    Question

  • I fairly new to WPF, I've been reading alot of articles and I'm kind of feeling a little overwhelmed now. I wanted to know what would be the best route to take if I wanted a button to change color when a certain value is true. Do I use a value converter, or a dependency property, or both? Triggers and setters? I'm going in circles here and nothing seems to work. I'm missing some logic. Here's what I have so far..
     my converter class
    Public Class pingConverter : Implements IValueConverter
    
     
    
     Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
    
      Dim state As Boolean = value
      Dim bckGrndColor As Color
      Select Case state
       Case True
        bckGrndColor = Colors.Green
       Case False
        bckGrndColor = Colors.Red
       Case Else
        bckGrndColor = Colors.Red
      End Select
    
      Return bckGrndColor
     End Function
    
     Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
      Throw New NotImplementedException
     End Function
    End Class
    
     
     my .xaml.vb file
     Public ReadOnly stateProperty As DependencyProperty = DependencyProperty.Register("IsSuccess", GetType(Boolean), GetType(MainWindow))
    
     Public Property stateValue As Boolean
      Get
       Return CBool(GetValue(stateProperty))
      End Get
      Set(ByVal value As Boolean)
       SetValue(stateProperty, value)
      End Set
     End Property
    
    
    
     Private Sub btnEPing_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnEPing.Click
      Dim pingsender As New Net.NetworkInformation.Ping
      
      Try
       If pingsender.Send(txtBxLocalIPAdd.Text).Status = Net.NetworkInformation.IPStatus.Success Then
        stateValue = True
       Else
        stateValue = False
       End If
      Catch ex As Exception
      End Try
    
     End Sub
    
     my .xaml file
     <Button x:Name="btnEPing"
              Background="{Binding Path=IsSuccess, Converter={StaticResource pingConverter}}"
             HorizontalAlignment="Left" 
             Content="PING"
             Margin="4 0 0 0"
             Padding="6 0 6 0"
             Grid.Column="2"
             Grid.Row="1">
            </Button>
    
     Thanks in Advance!

    my favorite place on the net is msdn!
    • Edited by msdner1 Tuesday, May 31, 2011 6:41 PM
    Tuesday, May 31, 2011 6:39 PM

Answers

All replies