I have a listbox that is being filter like this:
var view = CollectionViewSource.GetDefaultView(FilterSource);
view.Filter = FilterCode;
I am running into a problem where the SelectedItem is getting lost when the filter is used with viewmodel code like this:
VM
{
public ObservableCollection<Model> Items{get;set;}
public Model SelectedItem
{
get{return _selectedItem;}
set{_selectedItem = value; NotifyPropertyChanged();}
}
}
When the filter is applied, the SelectedItem is set to null. However, I want to keep that selected item unless the user actually clicks off of it. If the filter is removed, then the selected item should still be selected. The Model does have an IsSelected property, and I have been trying to think of ways to query for the IsSelected property. However, then the view's binding would not work the way I expect....or at least I am going in circles thinking it cannot
My only way of accomplishing a fix here is the following in the SelectionChanged event:
if (e.AddedItems.Count == 0 && e.RemovedItems.Count >= 0)
SpecialtyListBox.SelectedItem = e.RemovedItems[0];
But, this seems really hacky and forces that there must always be an item selected once an initial one is selected. In this case, that might work, but I would still like to see if anybody has a better solution?
I had a similar problem, where the listboxes were shown as tabbed views. I solved the problem by creating a Converter to produce a Boolean flag for "isActive" and assigning it to CollectionViewSource.IsLiveFilteringRequested. This prevented the non-active list boxes from updating the selected item.
You can fix this by creating a filter that always adds the current selected item to the filtered items. If this cannot be done directly, just hold the selected item in a separate variable.
Related
My listbox is having a grouped list so basically I want to find listbox group item index with item value. Listbox is having item source bind to it and is having DisplayMemberPath and SelectedValuePath set from code behind.
What I have tried yet are as follow:-
int index = istboxName.Items.IndexOf(ListBindToItemSource.particularParameterValue);
Give index=-1 always.
Another solution I tried is:
int index = ListboxName.Items.Groups.IndexOf(ListBindToItemSource.particularParameterValue);
Same result index=-1 always.
You should never need to access the items that way, instead access the item in your bound source and manipulate that. If you want to change anything in the view, like e.g. a Background, bind it on your item and change it at the source.
I have a combobox binded to an observable collection through
cmbBladesTab1.ItemsSource = easyRunData.olstBlades;
that works fine.
I want the combobox to be binded to all that values plus one.
E.g.
easyRunData.olstBlades; contains "PL1", "PL2", "PL3", "PL4"
while cmbBladesTab1 contains "ALL BLADES", "PL1", "PL2", "PL3", "PL4"
--ADD all work has to be done from code-behind
Thanks for your help.
You could add a property, that adds the particular item to the list.
ObservableCollection<string> myCollection;
ObservableCollection<string> MyCollectionViewProp
{
get
{
var tempCollection = new ObservableCollection<string>(myCollection);
tempCollection.Add("Extra element");
return tempCollection;
}
}
Depending on the size of the collection and the number of times it is accessed, this is probably the programmatically simplest solution. If you need to access it often, the worse this solution gets, as it creates a new collection every time.
In this case you should probably listen to the CollectionChanged event and keep a separate redundant list.
Easiest way would be to add an extra item in the observable collection with some prefixed text / key.
That way, because it's in the collection, it will be visible in the combobox and when the user selects this item you can evaluate it to see if it's the added item or not.
A good example is indeed given as an answer on this question add an item to combobox before bind data from data base
Here's my problem: I need to make a DataGrid with dynamic comboboxes using the WPF. If the value of a combobox is already used in the previous rows, the next ones, that will be added by the user, shouldn't contain the item already used.
In this image, the ITEM A shouldn't apear on the combobox of the second line.
I don't have ideia how to accomplish this, can anyone show me a light?
OBS: The DataGrid ItemsSource is binded to an ObservableCollection, and the DataGridComboBoxColumn ItemsSource is a List.
Thanks !!!
The ItemsSource of the combo doesn't have to be bound to an ObservableCollection, but it can help depending on exactly how you solve this.
When that cell goes in to edit mode the property the ItemsSource is bound to gets hit - so you can return a new list of items each time the getter is hit. Here is a very basic example to give you an idea:
public List<string> MyItemsSource
{
get
{
var myNewList = MyMasterList.ToList(); //create a (reference) copy of the master list (the items are not copied though, they remain the same in both lists)
if (PropertyA != null)
myNewList.Remove(PropertyA);
return myNewList;
}
}
So what you are creating and returning is a filtered version of your master list of all possible items. LINQ will be of great help to you here.
Alternatively you could keep just one static copy of the master list as an ObservableCollection, and simply remove items from that static copy as they get selected (and add them back in as they get unselected). Which option you choose will depend on how many times the list can be modified due to items being selected and how complicated it is to generate the list. I've used the dynamically generated list many times in the past, it's an option that works well in most cases.
I have a combo box that is bound to a collection that is essentially a list of Name/Value pairs. The collection can have multiple items with different names, but the values may be the same.
public class NameValuePair
{
public string Name { get; set; }
public string Value { get; set; }
}
public class NameValuePairCollection : List<NameValuePair>
{
public NameValuePairCollection(): base() { }
}
So inside my User Control I have a private field called items which is an instance of that NameValuePair collection:
private NameValuePairCollection items = new NameValuePairCollection()
Somewhere along the lines that collection gets initialized and items get added to it. However, the problem I see is when I try to set the selected index of the combo box that is bound to this collection:
this.CboItemsSelector.SelectedIndex = 3;
or
this.CboItemsSelector.SelectedItem = this.items[3];
The selected item is there but the UI is not synchronized. The UI's selector still defaults to the first item in the list, even thought the SelectedItem's Name and Value properties DO IN FACT CORRESPOND to whatever is in index 3 of the underlying collection!
Any ideas on how to force the ComboBox to refresh itself? Or just plain fix the issue? I know it's quite small issue, but it is big enough to force me to rewrite quite a bit of code.... :( :( :(
Thanks!
You need to inherit from ObservableCollection, not List. Otherwise no OnPropertyChanged events will be fired and the bound control wont know the data has been updated.
Do the selected Item's Name and Value properties match or is the SelectedItem an instance from within the same collection? .Net will not know to compare the items by name and value unless you tell it to, else it will use object equality to try and find the item in your list. If you are setting the selected item to an instance that is not actually in the list (but has the same properties) .net will not find it in the list. You have 2 options, override equality for your object and force comparison of properties, or ensure that you always set the selected item to an item in the list. Also try what Andy May suggested and do 2 way binding on the ItemsSource and on the SelectedItem, should work then
I cannot able to set focus on User Control Item
I am using DataGrid in User Control
I want to set focus on First row of DataGrid
I found following code
int index = 0;
dgAccountInfo.SelectedItem = winAccountInfoGrid.dgAccountInfo.Items[index];
dgAccountInfo.ScrollIntoView(winAccountInfoGrid.dgAccountInfo.Items[index]);
DataGridRow dgrow = (DataGridRow)dgAccountInfo.ItemContainerGenerator.ContainerFromIndex(index);
if (dgrow != null)
{
dgAccountInfo.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next))
};
By running above code I found ContainerFromIndex method always returns null
To rid of null problem I found following link
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ab95dd62-995f-481a-a765-d5efff1d559c/
I come out of null problem using above link but still focus is not set on data grid
Thanks for reading my question
It appears that, in the code you're using, you are data binding to something else, which is unnecessary if you are only wanting to set the focus to the first item. If your only intention is to set the focus (selected index) of the datagrid, that can be accomplished in one single block of code.
if (dgAccountInfo.HasItems){
dgAccountInfo.SelectedIndex = 0;
}
The if construct ensures that your datagrid actually has items in it (otherwise, the next line of code would throw an exception). If the datagrid has items, it executes the code inside the construct. If the datagrid has no items, it skips right over the code.
Even if you never plan to have no items in your datagrid, you should include this if construct anyway, in the off chance that something does happen
Inside the if construct, you set the datagrid's SelectedIndex to the first item. Remember that, like with a list box, a datagrid is "zero-based," meaning that the first item has an index of zero. To select nothing in the datagrid, set the selected index to "-1"
In the end, setting "SelectedIndex" is far more reliable than setting "SelectedItem," as the items in a datagrid usually change.
If you wanted, you could include an #else statement in the above code (just above endif) if you wanted to do something else if the datagrid has no items.
I hope this helps!