Hello, please tell me why and what are the conveniences of Converter 'and in Binding '?

  • You store in the base bytemassiv, on the form you display a picture. To display using the converter. - Monk
  • @Monk and you can examples of converters, you can just links) - alex-rudenkiy
  • @Monk I will expand the question a bit: why do we need a converter in the markup, if it is possible to convert it into a VM? - andreycha
  • @ alex-rudenkiy Well, I’m especially lazy to google, except that I have a raw version here - github.com/MonkAlex/MangaReader/blob/AvaloniaUI ... - Monk
  • @andreycha baytmassiv as a representation of a picture, each view can itself think about how to display - otresayzit, not to display. Plus, for example, pictures in WPF cannot be fumbled, i.e. it is necessary not to forget to frizz, but if you suddenly draw on some kind of GDI, there is also a limit on how many objects can be built. What if no one requests a display? There is also list virtualization. In short, to dump part of the load on the converter in the case of pictures - it will even go very well. - Monk

2 answers 2

Converters are needed to not render the presentation logic in the VM. VM, for example, should not want to know the width of the table column, the current language, the selected application skin, and similar clean view details. Here for converting VM-objects into a view and converters are needed.

Examples

You have a VM object meaning a date. Its type is, of course, DateTime . And in the UI, you want to show it as “today,” “yesterday,” “a week ago,” “April 1,” or something else. Translation into such a notation is a good place for a converter.

You need to print "15 copies", but if the column is too narrow, then you need to reduce the text to "15 copies." Dealing with column sizes is not a matter of VM, write a converter for this.

In VM, you have the language of the message, and in View you decide to show it as a flag of the desired country. Converting CultureInfo to a pack URI to your graphic resource is not a VM task, leave it to the converter. VM does not know anything about graphic resources at all.

In VM, some data is sometimes missing (the value of the property is null ), and you do not want to show a frame with a beautiful background for empty data. Turn null into Visibility.Collapsed - the converter and only it.

You have a cell of the game board, and you need to find out its coordinates. (Assume that your cells are not square, and the grid does not automatically fit.) The size of the cell depends on the size of the window, the number of cells, and some other purely visual details. Who will recalculate the coordinates? VM? Figushki, this task should be solved by the converter.

You have a custom progress bar, it's not linear, but round. Someone needs to recalculate the position of the progress bar to the corner. VM? No, VM knows nothing about the style you gave to the progress bar. And exhibiting properties for round, square and heptagonal progress bars is completely non-semantic. Therefore, let the progressbar style use the converter.

You need to bind the width of one control so that it is equal to twice the width of the other control. Who will multiply by two? Bring the task to the VM through 18 bindings? Just add a converter.

In general, for converters there are many tasks of a purely visual nature.

  • VladD, I wonder how you manage to answer so many questions a day? ) - sp7
  • @ sp7: I'm surprised myself! :) - VladD

A simple option is to actually do all the necessary properties in a VM and just bind on them in View .

As long as it suits you, you do not need a converter.


On the other hand, it is very often necessary to work with something, for which, for example, there is standard input data and standard display.

As an example - Markdown markup, which has quite a standard of writing, but it’s inconvenient to print with bare text, so the converter from Markdown to FlowDocument is quite useful.

Another situation when the converter comes in handy often is images. The main way to store and transfer binary data is an array of bytes, to see it in the VM is quite a normal situation. Accordingly, the display will require a converter, which, for example, is sharpened to a specific image format, size, or some other settings.

You can always use the converter is not quite as intended. I met this rarely, but nonetheless. For example, to enter a conditional phone number by mask, the number will be stored and transmitted in the form of numbers only, and displayed in some type of +8 (999) 123-34-56.