|
So I must reveal my ignorance ... I never learned about "binding converters".
How does a "binding converter" work? What does it do? How do you use it?
|
|
|
|
|
This is a very glossy overview
I assume you know what binding is since you are watching this forum on WPF. Let’s say you want to bind a bool to visibility. Bool has only 2 states, true and false. Visibility has 3 states, Visible, Hidden, and Collapsed. (Ignoring null). A binding converter will convert bool to visibility.
As stated, before binding and converters are not for the faint of heart. This link will get you started, barely.
Value conversion with IValueConverter - The complete WPF tutorial[^]
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
OK, I have used and written that kind of converters. I just didn't trigger on the name "binding converter". I though it was some way of converting the binding as such, not the value type!
|
|
|
|
|
michaelbarb wrote: they are quite dated
Really? The first result[^] was updated two years ago. WPF hasn't changed that much since 2006.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I've always written my own converters. I have about a dozen or so. I have a bool-to-visibility converter that can be configured (in XAML) to set the visibility to any of the three possible values according to the bool value (just as an for instance).
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
What do you want to convert?
I rarely have to convert (some yet unidentified) BCL class to other BCL class. Usually I need to convert a business class (i.e. local to your code) to perhaps a BCL class (such as color, image source, etc).. You need to write your own converter there (come on, it's only 2 methods to implement, or just 1 in case of one way binding).
Also, if you make some view model, view model can directly expose a property of the right type and have value conversion happening in the property setter.
|
|
|
|
|
I recently did an app and the company library had a set of about 30 converters in it. Mostly common things. I found myself using binding and converters much more than I ever had in the past. Simply because I did not have write the converters. They are not hard to write, it is just I am lazy. It got me wondering if out there somewhere there is a better worked out converter library????
So many years of programming I have forgotten more languages than I know.
modified 14-Dec-21 10:33am.
|
|
|
|
|
forget it... converter are the simplest thing to write. if you feel the need to download premade one, you need more practice writing ones!
also, most of them, apart perhaps from the classical boolean to visibility, null to boolean, and null to visibility and color to brushes and pens, are specific to your application
|
|
|
|
|
I have two ListBoxes on the screen, and this is my requirement:- Upon loading the window, the first listbox (ListBox1) should be populated with items (which is happening correctly), but I also want the first item to be selected. This is to happen via MVVM, and not through codebehind. And upon selecting in the first listbox (ListBox1), the second listbox (ListBox2) should be populated (which is happening correctly), and its first item (of ListBox2) is to be selected.
How is this listbox selection to be achieved via MVVM?
modified 21-Nov-21 0:50am.
|
|
|
|
|
Your listbox1 needs to handle the SelectionChanged event, which when executed, populates the collection for listbox2. Since listbox2 is bound to the appropriate collection, it will automatically update itself.
You didn't say whether or not you wanted to retain the selected item from listbox1 in the viewmodel.
I'm assuming that your bound collections for both list boxes are ObservableCollection s...
To automatically select the first item in listbox1, simply set the SelectedIndex property to 0.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Ahh ... but you didn't say "where" to set index to 0. He wants "no code behind". I guess you can set it in XAML, but isn't it "0" anyway? (Actually, I thinks it's -1 when newly loaded. Shrug)
Just taking digs at "no code behind" MVVM; not your answer.
It's "plumbing". To say you can't put it in a loaded event (as "code") is OCD, IMO.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
"No code behind" is a pointless endeavour, and a highly artificial requirement. It also means that he app is virtually impossible (or at least a pain in the ass) to debug. I make it a practice to put as much as possible in the code behind for this very reason. Sure, control interaction can be put into the XAML many times, but I pretty much use code-behind for everything.
In the real world, you do what works, and use the simplest means possible. Doing the work is no time to exercise classroom theory or hypotheticals. Experienced programmers (like us) are cognizant of these facts and are much more efficient coders as a result.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Thanks for your replies.
In fact, I have a fully working Codebehind solution, which is quite less code, and is working perfectly well. I was asked by my manager to convert it to MVVM, and am facing somewhat unsurmountable difficulties, in making it non-codebehind. That was the reason for my question. Now, having listened to the opinion of experts like you, I will convince my manager that some codebehind is not taboo.
|
|
|
|
|
To be clear, MVVM does not mean no code-behind. It’s about separation of concerns. The model loads and saves the data. The view model both transforms the model for, and eases interaction with the view. The view binds the view model to the ui. MVVM can be implemented in such a way as to couple the three concerns as loosely or as tightly as is necessary.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
After yesterday's demo, the requirement got changed. Instead of two listboxes, one listbox and one datagrid is what is needed. So, I am off to understanding datagrid programming now. The weird software world we are all in
|
|
|
|
|
There's also an inclination to get carried away with "data grids". They're useful for browsing but they'll suck up all your development time if you (try to) use them for data entry instead of using a "form" (for the data entry / update part).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I use ListViews. In fact. I wrote an article on CodeProject that illustrates a custom ListView control that can auto-generate columns based on the dataset being represented. It doesn't support editable cells, but it does have sort functionality (that can be toggled on a column-by-column basis, column name overrides, custom column formatting, and other stuff.
Auto-generated columns in a WPF ListView[^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Hi,
I want to create a digital clock at the top right of my application. I wrote backend codes for the clock and the clock works fine. But, I want to use custom digital font for the clock. I downloaded the font from DS-Digital Font | dafont.com[^] and added them to the fonts folder in my project. I used Resource as build action and selected Do not copy. The App.xaml is as follow:
<FontFamily x:Key="Digital">pack:
and MainWindow.xaml:
<Label Name="TimeLabel" Grid.Column="1" Foreground="White" FontSize="24" FontFamily="{StaticResource Digital}">00:00:00</Label>
The problem is that the font is not applied to the label.
Please help me.
Thanks.
|
|
|
|
|
Try this:
<Window.Resources>
<Style x:Key="Digital" x:Type="Label">
<Setter Property="TextElement.FontFamily" Value="Fonts/#DS-DIGI" />
</Style>
</Window.Resources>
And then set the style of the control to that style.
<Label Name="TimeLabel" Grid.Column="1" Foreground="White" FontSize="24" Style={StaticResource Digital}">00:00:00</Label>
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I'm getting error: the attribute 'Type' from the XAML namespace is not Defined in the first code block you gave. How can I resolve it?
|
|
|
|
|
Change it to TargetType="Label" .
This is what I get for giving a code example without typing it out in the IDE first.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
This method cannot change the font. I think there is something with fonts. Some fonts cannot be used in WPF.
|
|
|
|
|
Try this one:
Font Empire - Digital Readout[^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
DS-Digital works fine in WPF. I'm using it in a tool I wrote last year.
I added the font .ttf files to a Fonts folder in my project. Set them to "Resource", NOT "Embedded Resource".
In my XAML, I've got the following:
<Window.Resources>
<FontFamily x:Key="ValueFont">./Fonts/#DS-Digital</FontFamily>
...
</Window.Resources>
...and wherever I want to use the font:
<TextBlock ... FontFamily="{StaticResource ValueFont}" ... </TextBlock>
Of course, you can always use the same font path in a Style block with a FontFamily.
modified 15-Nov-21 9:21am.
|
|
|
|
|
I have a set of five buttons, in a StackPanel, which is a user control. I would like to use this same StackPanel in two different places on the screen, with different orientations - Horizontal in one place, and Vertical in the other place. Is it possible to do it in WPF?
(I can have two different user controls, but most of the code will be same between the two, the only difference being in their orientations. I would like to reuse the StackPanel in both these places, with different orientations).
Thanks in advance.
|
|
|
|
|