Change Background Color for WPF textbox in changed-state

前端 未结 6 1652
故里飘歌
故里飘歌 2020-12-15 01:19

I have a class EmployeeViewModel with 2 properties \"FirstName\" and \"LastName\". The class also has a dictionary with the changes of the properties. (The class implements

6条回答
  •  我在风中等你
    2020-12-15 01:49

    Just use a MultiBinding with the same property twice but have Mode=OneTime on one of the bindings. Like this:

    Public Class MVCBackground
        Implements IMultiValueConverter
    
        Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
            Static unchanged As Brush = Brushes.Blue
            Static changed As Brush = Brushes.Red
    
            If values.Count = 2 Then
                If values(0).Equals(values(1)) Then
                    Return unchanged
                Else
                    Return changed
                End If
            Else
                Return unchanged
            End If
        End Function
    
        Public Function ConvertBack(ByVal value As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
    End Class
    

    And in the xaml:

    
        
            
                
                
            
        
    
    

    No extra properties or logic required and you could probably wrap it all into your own markup extension. Hope that helps.

提交回复
热议问题