question

ZachariasKarasavvas-3275 avatar image
0 Votes"
ZachariasKarasavvas-3275 asked ZachariasKarasavvas-3275 commented

Question about performance in Wpf

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

dotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered ZachariasKarasavvas-3275 commented

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

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the answer Peter. That means that every attribute I want to bind in Xaml should use the actual interpretation? For eg alignment, should I use not the string datatype but the Horizontal alignment enumeration? And so on?
Does this affect the performance?

0 Votes 0 ·

Hi,
the best way is to use double type, see FrameworkElement.Width Property.


0 Votes 0 ·

Thank you Peter

0 Votes 0 ·