Question about performance in Wpf

Zacharias Karasavvas 21 Reputation points
2020-12-31T21:09:15.843+00:00

Hi,
I have a general question about the right way to bind a class to a listview or list box or any other control in wpf.
Lets say that I have a textbox which the width is binded with the property of my class "my width"
<Textbox x:Name="mytext" Width="{Binding mywidth}" />

My question is how do I open the property in my class. As a string property or as an int.
Xaml won't mind and will translate both but which is more accurate and proper?
Same goes if I want to bind for example the Horizontal Alignment. Should I use a property of type of string or should I use the Horizontal Alignment enumeration?
Do we have any delays while Xaml converts the enumeration or should I leave everything as string data types which is kinda default in Xaml?
Appreciate a guidance in this

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
768 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-01-02T11:23:13.38+00:00

    Hi,
    you can check the performance with this code:

    Imports System.ComponentModel
    
    Namespace WpfApp072
      Public Class ViewModel
        Implements INotifyPropertyChanged
    
        Public Sub New()
          start()
        End Sub
        Public Property HAlignment1 As String = "Left"
        Public Property HAlignment2 As HorizontalAlignment = HorizontalAlignment.Left
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
        Private Async Sub start()
          Await Task.Delay(1000)
          Dim sw As New Stopwatch
          sw.Start()
          For i = 1 To 10000
            If HAlignment1 = "Left" Then
              HAlignment1 = "Center"
            ElseIf HAlignment1 = "Center" Then
              HAlignment1 = "Right"
            ElseIf HAlignment1 = "Right" Then
              HAlignment1 = "Left"
            End If
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(HAlignment1)))
          Next
          Debug.WriteLine($"String: {sw.ElapsedMilliseconds} ms")
          sw.Restart()
          For i = 1 To 10000
            If HAlignment2 = HorizontalAlignment.Left Then
              HAlignment2 = HorizontalAlignment.Center
            ElseIf HAlignment2 = HorizontalAlignment.Center Then
              HAlignment2 = HorizontalAlignment.Right
            ElseIf HAlignment2 = HorizontalAlignment.Right Then
              HAlignment2 = HorizontalAlignment.Left
            End If
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(HAlignment1)))
          Next
          Debug.WriteLine($"HorizontalAlignment: {sw.ElapsedMilliseconds} ms")
          For i = 1 To 10000
            If HAlignment1 = "Left" Then
              HAlignment1 = "Center"
            ElseIf HAlignment1 = "Center" Then
              HAlignment1 = "Right"
            ElseIf HAlignment1 = "Right" Then
              HAlignment1 = "Left"
            End If
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(HAlignment1)))
          Next
          Debug.WriteLine($"String: {sw.ElapsedMilliseconds} ms")
          sw.Restart()
          For i = 1 To 10000
            If HAlignment2 = HorizontalAlignment.Left Then
              HAlignment2 = HorizontalAlignment.Center
            ElseIf HAlignment2 = HorizontalAlignment.Center Then
              HAlignment2 = HorizontalAlignment.Right
            ElseIf HAlignment2 = HorizontalAlignment.Right Then
              HAlignment2 = HorizontalAlignment.Left
            End If
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(HAlignment1)))
          Next
          Debug.WriteLine($"HorizontalAlignment: {sw.ElapsedMilliseconds} ms")
        End Sub
      End Class
    
    End Namespace
    

    Result:

    String: 42 ms
    HorizontalAlignment: 25 ms
    String: 67 ms
    HorizontalAlignment: 38 ms


0 additional answers

Sort by: Most helpful