How to get the updated selected item in WPF? - c#

I had to refresh the items of a TreeView as some of the changes made were not reflected in the code behind. There are about 15 parent items and each parent has around 50 - 100 child items.
treeView.Items.Refresh();
treeview.UpdateLayout();
This is called every time a search is made on the tree (textbox search).
Will it affect the performance?
EDIT :
I set the selected item of the tree in the ViewModel. Somehow for some cases(random), even after setting a new selected item, the change is not reflected in the view.
treeView.SelectedItem - remembers the previously selected item and returns the previous selection. It returns the new selection only if I call UpdateLayout() on the tree before I get the SelectedItem. This in turn affects the performance. What can I do to get the updated SelectedItem.

Related

Listbox with checkboxes - code seeing every item IsChecked = true

I have a WPF Listbox which contains a list of checkboxes which are all named as the names of other controls in another window.
When the listbox is looped by grabbing each item in lst_control.Items:
_details.controlIDs.Clear();
foreach(Control item in lst_controls.Items)
{
if (item.IsChecked)
//Add item to list
_details.controlIDs.Add(item.controlID);
}
The code is fired on a check/uncheck of any of the checkboxes within the listbox. It sees each item.IsChecked as true - even if it is unchecked.
EG: Check the top box in the list, it sees it as IsChecked = true, but it also does for every other control in the list.
Weird behavior - has anyone seen this before?
Even if you're not going to go the full MVVM route, this issue is best resolved by separating the UI and the data layers in your application.
Make a data item class that includes an IsSelected boolean property, and set the ListBox's ItemsSource to a collection (eg ObservableCollection) of these items. For two way data binding, the data item class should implement INotifyPropertyChanged.
In your UI, make a DataTemplate for the ListBox's ItemTemplate property, that includes a CheckBox that is bound to IsSelected.
That way you can scan the collection of data items (using Linq or otherwise) to find those that are selected.

Selected ListViewItem is not highlighted on TabPage, after selected TabPage changed forth and back

I am currently facing a strange behavior of the C#.Net ListView when used on a TabControl's TabPage.
Background:
I have said TabControl with three TabPages, the ListView is on the first one.
Scenario:
I select one Item in the ListView and its "Selected" property is set to "true"
I change to another TabPage
I change back to the first TabPage with the ListView containing the selected Item
Result:
The former selected Item in the ListView is not highlighted any longer. Iterating over all Items shows that its "Selected" property is still set to "true".
As soon as I click into the ListView control, I can see the selected Item being highlighted for a short moment, before loosing its highlighting again due to me having clicked somwhere else and therefore deselecting it.
But neither one of Refresh, Invalidate, RedrawItems, Focus or Select on the ListView leads to the selected Item being highlighted again.
Has anyone of you experienced the same issue and found a proper way to solve this?
My current solution is to clear the List of SelectedItems on the ListView as soon as the corresponding TabPage is re-focused, hence deselecting every Item and forcing the user to re-select what he wants to be selected.
But since I also noticed that ListBoxes can savely store their selection and display it properly, I would wish for the same feature on the ListView.
Thanks,
Thomas

Silverlight ListBox not updating when not in XAML tree

Simplified version of the problem. So concentrate on the technical issue not user friendliness. ;)
I have a listbox filled with items from an ObservableCollection.
It is showed in my first view.
By selecting an item in the list the user goes to a another view.
I swap this view in, so the first one is not in the xaml tree.
In the second view they can perform actions.
One of the actions makes the item go away from the top list (like passing the item on to another user).
The first view listens for that event and can remove the item from the list.
Items.Remove(item)
Problem is that the item doesn't always disappear from the ListBox (but it does from the underlying list, I've checked with debug). I think it might have something to do with the view not being displayed at the moment the collection is updated. I've tested with setting SelectedItem to null, thinking that selection might keep it there after removal, but it made no difference.
Is this a known limitation, having to be visible for the listbox update to take effect?
Can I trigger something to make the observablecollection re-fire the notification?

WPF TreeView doesn't show content

I have a WPF application that includes a TreeView. The user adds content to the tree using right click and context menu.
This creates a new TreeViewItem that is added to the tree's ItemCollection.
The problem is that sometimes the new items are not shown, although they were added. If you minimize/maximize the window they suddenly appear.
I tried to call TreeView.UpdateLayout() after the addition of the new item, but the result is the same.
Any suggestions?
dont add to the trees item collection. bind the treeview to an observable collection. keep the model and the view seperate. the treeview will reflect changes to the model. you should never have to call treeview.updateLayout. add your items to a collection. (your model) which is bound to the treeview. its a lot less work
I am not shure about this but try calling Refresh or call on the node witch is parent to the new node ExpandAll. Hope this works for you
Best Regards,
iordan

How do I get Count of parent collection from child?

I've got an ObservableCollection assigned to ItemsSource of a listbox. Listbox is using a DataTemplate which has a usercontrol in it which has items bound to each listboxitem's properties.
I have an up and down button on the usercontrol which moves an item up or down the list. The list is sorted by the property that I'm changing. Click up or down, the DisplayOrder property is changed, I'm using INotifyProperty to tell the ObservableCollection it needs to re-sort.
What is the best way for the usercontrol to get the item count so that I can disable the down button when an item reaches the bottom of the list. (The top is easy, I compare to 0)
I see two ways of handling this.
The first is to pass a handle of your collection to each of your items (when they get added to the collection) so that they can calculate if they are the first or last item themselves.
The other is to expose writable properties on your items, such as CanGoUp and CanGoDown, and your parent control becomes responsible for setting these properties properly. I prefer this solution because it decouples the behavior of your parent list, from the child items. Even though the up/down buttons are placed on your child items, it's really a functionality of the parent list.
listBox1.Items.Count ?
this.Parent.Controls.Count?

Categories

Resources