Refreshing WPF ComboBox - c#

I have ComboBox that lists all enum values using ObjectDataProvider. Since enum-values are not always that clear and they need to be localized, I use converter to do that. This all works fine, but when I change the language, I have to somehow update the combobox. How I can do that? ComboBox'es UpdateLayout() and Items.Refresh() have no effect at all.

Try calling Refresh on the ObjectDataProvider itself.

Related

What is the use of RefreshItems() from Listbox in c#

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.

Binding controls to a collection of objects

I need help with data binding. Imagine this situation. I have two classes, one named Isotope, another named Photon. Class Isotope contains BindingList Photons. I also have a static class StaticVariables, where I put BindingList Isotopes. Now, I want to make a form which will allow me to browse the list of isotopes. I created a combobox CBIsotopes, that I bound to StaticVariables.Isotopes:
CBIsotope.DataSource = StaticVariables.Isotopes;
CBIsotope.ValueMember = "IsotopeName";
CBIsotope.DisplayMember = "IsotopeName";
So far, everything works. Now I want to create a datagridview DGVPhotons that will show all the photons of the selected isotope. My first instinct was to do something along the way of
DGVPhotons.DataSource = StaticVariables.ListOfIsotopes.Photons
which of course, doesn't work. Another thing I tried is to use SelectedItem property of the ComboBox:
(1)
DGVPhotons.DataSource = (CBIsotope.SelectedItem as Isotope).Photons;
This works, but not as well as I would like. If I do it on load time, nothing happens, because ComboBox is empty. If I do it when an item is actually selected in ComboBox, then it works, but as I change the selection in ComboBox, DataGridView stays the same. The solution would be to put line (1) in SelectedIndexChanged of the ComboBox, but it seems like a brute force method to me, and I feel that my approach is fundamentally wrong... Is there some more elegant solution?
Ok, the key here is to use DataBind solution. After you change source of your element don't forget to use DataBind method after, in order to bind new data.
And also, on PageLoad event, don't forget to use IsPostBack sign in order to initialize page only when request is handled for the first time.

Is it possible to change gridviewcolumns during runtime using mvvm?

I'm binding to an ienumerable(items) in my MainViewModel to display data. As i've described here before How to change the content in a datagrid or listview using MVVM, all i want to do is display different tables. My first approach was to use the normal datagrid and set the "AutoGenerateColumns" to true, so that the correct columns are displayed. As it turns out, performance was pretty bad so i switched to ListView and GridView, but since there is no "AutoGenerateColumns" available i need to somehow create and change the columns.
So how would you do it?
Have a look at this question for something that comes close to -- but is not -- auto-generation: WPF GridView with a dynamic definition.
Generally speaking, autogenerating means doing reflection on the ItemsSource, which can get messy since it needs to cover a huge number of possibilities correctly.

Set a hidden property to a ListBox Item - C#

hey,
Does anybody knows a way to set a hidden value to a ListBox Item.
As an alternative i can use another listbox simultaneously.
Thanks.
You are probably looking for the Tag property. It holds an object type, which means you can save a reference to anything in there.
Although before setting on using the tag property, take a look at ItemsSource. Basically you can tie a collection of things to be the provider for your listbox, it's usually a superior alternative to dealing with Listbox items individually.

How to make a TreeView's and ListBox's SelectedItem stay in sync?

I'm certain this has come up before, but I haven't been able to find the answer.
I made a basic ViewModel that contains a list of People (an array of Person) with a property called SelectedPerson, which naturally points to the currently selected Person in the list of People. I also have a ListBox and a TreeView that are databound to the ViewModel's People list.
What I'd like to do is to keep the ListBox's SelectedValue and TreeView's SelectedItem in sync with with the ViewModel's SelectedPerson. The idea is that no matter how the SelectedPerson is modified (through a control, through code, etc), all the controls should update themselves properly. I can get it to work with two ListBoxes, which is nice, but I can't get it to work with a ListBox and a TreeView because the TreeView's SelectedItem is readonly and apparently unavailable through XAML.
Where should I look to get ideas on making this work?
Also note that I'm trying to make this work in pure XAML. No code-behind as XAML files in my application can be loaded and changed dynamically.
Thanks!
You can Use Selector.IsSyncronizedWithCurrentItem.
You can bind both thye listbox and treeview to the same datasource and make sure that the IsSyncronized parameter is set to true. Then any changes to the current item in one will be reflected in the other.
More information can be found here:
link text
I asked around and the best solution I could find was here
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cc73893a-3383-4328-a002-ed8fb002a19d
It works for me but it's not the most optimal solution at this point.

Categories

Resources