so I want to populate a combobox items source with the contents on an enum (and obviously retrieve the values later on).
Populating works fine with either of the two following approaches but neither matches what I want to achieve.
comboBox.ItemsSource = Enum.GetValues(typeof(VirtualKey));
I can retrieve the values without issue using a simple (VirtualKey)comboBox.SelectedItem. The problem is, the names in the dropdown menu are all unreadable.
comboBox.ItemsSource = Enum.GetNames(typeof(VirtualKey));
Displays the names as intended but I can't retrieve the value like with the other instruction.
Any ideas how to solve the situation?
comboBox.ItemsSource = Enum.GetNames(typeof(VirtualKey));
Displays the names as intended but I can't retrieve the value like with the other instruction.
I don't know how are you getting the selected value, but for converting from enum value name to its value, you can use the Enum.Parse(Type, String) method.
Or you can bind the ComboBox.SelectedIndex to your enum variable for example using something like my EnumToIntConverter.
This puzzling behavior was also discussed here:
UWP - binding Enum differences
Even simple ToString() call overcomes this issue. For some reason however, some Windows Runtime enums in direct XAML binding show up with IReference.
Related
In winforms c#, there is a control named ListBox. It has a method RefreshItems()
What is the real use of this method ? can someone explain with example and sample code ?
According to the documentation it does the following:
Refreshes all ListBox items and retrieves new strings for them.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.refreshitems?view=windowsdesktop-6.0
Which is a bit vague.
If we look at the RefreshItem(Int32) method, it has a remark which clears things up a bit
If the DisplayMember property is set and the property in the data source that is assigned to DisplayMember changes, use the RefreshItem method to update the value in the ListBox control.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.refreshitems?view=windowsdesktop-6.0
This suggests that if you've set a display to an item, and update the display string, you want to call the RefreshItem so that the UI will show the correct value.
RefreshItems is the same, but for all items.
I need to take the currently SelectedIndex of a Listbox and pass the index number to my ViewModel. That index number is then used to access a specific object in a list of objects which is then used as part of a formula. Most examples I've looked at recommend using SelectedItem instead of SelectedIndex, but the Listbox is comprised of bitmap images so I'm not really sure how that would work.
Here's an example of how things are supposed to work:
user selects one of the images, let's say the very first one, so SelectedIndex would be equal to 0.
the value of 0 gets passed to the ViewModel which has a List of "Record" objects.
since 0 was passed, the first object in the list is accessed and one of its properties is used in a formula.
This seems like a pretty straightforward thing to do, but my novice experience in MVVM and WPF has me a bit stumped.
I think you can bind a property(let's assume it's called SelectedIdx) in ViewModel to SelectedIndex , Binding Mode can chose OneWayToSourc ,or TwoWay.
So,when you selected one item,your viewModel will get the selectedIndex through SelecteIdx.
I have a ListView with a GridView (multiple columns) layout. Some column have converters which uses the current locale to prettify numbers, turn unix timestamps into datetime strings, or just translate an enum to a localized description of it.
The locale can be changed during runtime, so I need a way to rerun these converters as that happens. Note that the value itself of the binding has not changed, but the output of the converter may be different with a different locale.
What is the best way to do so? I don't want to iterate the whole collection of every affected list and call OnPropertyChanged. Is there a way to either force the ListView to refresh every binding, or just the bindings of some columns?
I found the answer:
ICollectionView view = CollectionViewSource.GetDefaultView(MyListView.ItemsSource);
view.Refresh();
You can force the ListView to update the ItemsSource binding with the following:
lview.GetBindingExpression(ListView.ItemsSourceProperty).UpdateTarget();
Using this same syntax, you could certainly get more specific, and force an update only on the bindings for specific columns, but depending on the number of rows in your ListView, you may not see a performance difference.
I have a listbox I'm setting with a datasource of highscores
public class HighScore
{
public string Username {get;set;}
public int Score{get;set}
}
var IList<HighScore> HighScores = getAllTheScores();
MyListbox.ItemsSource = HighScores;
I want to change the background color of any rows which have a Username property equal to the currently logged in user (stored in AppSettings). I've seen Converters but this would need to somehow get hold of the currently logged in user which doesn't seem like something a converter should be responsible for getting.
I could also iterate the listbox items but from what I've seen that's not advised and I should be preferring binding to code behind drilling into controls.
Any suggestions as to how best achieve this much appreciated :)
Two suggestions. You already mentioned the first one, which is to use a ValueConverter. If value value is accessible from a viewmodel (recommended) than that is easy to pass along to the ValueConverter. If it is not stored in a view model then there are no worries with getting this from within the value converter provided it's named properly.
The second suggestion is to use a Behavior. The Behavior would be responsible for setting the background based on the user. I don't see much advantage here over a value converter except for the back that it's not a value converter. It would (almost) do the same thing. Overall I would recommend a value converter. Easy to implement, low code overhead and little xaml needed.
I have a ComboBox whose ItemSource is bound to a list of strings (idealy i would use an Enum), this is done using the MVVM pattern.
Now i want to bind an object to the ComboBox, it's called SelectedUser and i want to bind its property: UserType, which is a string.
So i have got this:
<ComboBox ItemsSource="{Binding Path=Usertypes}" SelectedValue="{Binding Mode=TwoWay, Path=SelectedUser.UserType}" />
It works and it does change the value of the selected user if i play with it, but the problem is, that it does not display anything in the ComboBox unless i select a user, and then change the ComboBox selection, then it works, but only for that user.
I tried playing around with DisplayMemberPath, SelectedValuePath and SelectedItem,
when i added those the ComboBox did not show anything in it (there were still options to select from, but they were invisible or something).
So what should i do? Is this a bug?
I have to mention that i have got another ComboBox that has a list of ints, and it works fine.
Update:
I was informed that I'm getting this issue because the string I'm comparing to the string in the comboBox, are not actually the same.
My string comes from the Entity Framework via Ria Services. (User.UserType)
And when it compares it to the list of strings in the ComboBox ItemSource, they are not equal, for some strange reason.
And i also heard, i might have to override Equal method for that check.
but I'm not sure where and how to do so.
Is the view notified if the SelectedUser changes? I could imagine that this might be the problem; if there is no such notification the ComboBox will not reload the SelectedValue if another user is selected, it will only update the binding if you make changes yourself.
If that is not it, you also need to consider that no selected value will be displayed unless the current value exactly matches one of values in the source list.