简体   繁体   中英

WPF Binding Constant Variable Vs Static Property

I am trying to bind constant values from ViewModel to the View. Should I choose constant variable or static property? Both works but is there any advantage of one over the other?

Edit

My intention was to move the ValueConverter logic to properties in viewmodel. Hence whichever the static values used by the converter I am moving them to my view model. Hence the question.

Since you are suggesting to keep them in the view. I would like to ask another question should I move the converter logic to ViewModel? Is there any downfall?

If it truly is a static unchanging value then I would suggest that you use a static instance of the value in XAML. There is no real performance difference, but there is a maintainability difference - the value is not obscured in any way and is immediately traceable in the XAML.

What you have to remember is that technically you should be able to bind any viewmodel to your view - that is one of the purposes of the pattern (to decouple views from viewmodels and other working code). What would happen if you stored this unchanging value in your viewmodel and then changed the viewmodel out at some stage, and the new viewmodel either had a different value or no value at all?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM