WPF TreeView Cancel TreeViewItem Selection - wpf

I've been looking for a solution to this problem but I still can't find an adequate solution yet. Please help. Basically, I have a treeview that's bound to a list of objects. When the user clicks on a treeviewitem, I load data for a data grid based on the treeviewitem. The user can change data in the grid. If there's a change in the data grid and the user clicks on another treeviewitem, the user is asked to save. The user can choose Yes, No, or Cancel. When the user chooses cancel, the application has to make the user stay on the current treeviewitem until he/she saves the data grid. The problem is once the user has selected another treeviewitem, I can't make it so that the previous selection is selected again. There is no "SelectedItemChanging" event. WPF treeview only has SelectedItemChanged event. I'm trying multiple solutions but I always end up with an infinitely. Please offer any tips or advices.
Thanks,
Minh

You could just use a private field in which you store the item which was selected last, just handle the SelectedItemChanged event and always update the field as required. Also as Dave Clemmer suggested having an IsSelected property to which you can bind helps with selecting items programatically.

Related

Changing the data in a specific area of a window, Caliburn.Micro, WPF

Here is my need. I think a user control is what I need but I am not sure if its the best or even how to use it.
What I have. My main window has a menu with a "help" menu. When you click help, a new window opens, I have a column, At the top of the left column has a drop down box of "Major Titles", a ListBox below that that populates based on the combo box selection. This will be about 25% of the window width. All this works.
When I select an item in the list box a page,contentControl or user control is displayed to the right with verticle scroll bars so the window does not need to resize, the information I display that changes based on the list box selection will have only visuals like text block, label, images. There will be no user interaction with the changeable pages Just formatted Data.
What would be the best way to approach this? Can anyone offer an easy example?
I was thinking of using a user control and change the user control based on the selected list box item.
Ok, I got this figured. After reading a lot of posts and blogs. Seems people sometimes want to make things more complicated then they actually are.
What I wanted, a permanent list box on the left 1/6 of the window. The list box contained string names for "help subjects". On the right 5/6 of the screen I added a groupbox with header and in group box I added a usercontrol. The content of the control is bound to a property called 'ActiveView'. The list box selected value property is bound to 'SelectedListItem' property.
When you change it set 'SelectedListItem' a method is called 'SetActiveControl'. SetActiveControl has a switch/case that sets like in the example:
``Case "Setup":
ActiveView = new SomeSelectionViewModel();
Break;
I have created a user control in a folder inside the Views folder called HelpControls, I also created the same folder in ViewModels. I have classes matching the user controls and everything is bound together.
Ultimately when you click the list box item, the associated ViewModel is called and in turn populates the user control on the window with the appropriate data.
I need to later look into, using one ViewModel for all the controls, I know that can be done using Cal:Model.View = ViewModel name. In the xaml of the control. I'm just not sure how to call the appropriate user control view when an item is selected. Either way this would become a view first design and I thought I read, Caliburn. Micro was intended as a ViewModel first design.

Setting Focus to a Control after the Loading of the DataGrid

I have a UserControl in WPF and it contains a variety of controls in it... The most important being the DataGrid Control. The DataGrid control is bound to an Observable Collection list. This list filled with different items based on couple of filters selected by the user. Now, once the DataGrid displays all the data. I want to set the Focus on the first (Filters) combobox on my usercontrol. Is there any way to know when the DataGrid has completely loaded??? Is there any DataTrigger I could set that i could trigger to inform me that Grid was refreshed with new list and I can set the ComboBox to focus. Basically i'm not able to set the focus to the first control on my UserControl when the data in the DataGrid has been repopulated... Please let me know if anyone knows how to resolve this issue!!
Thanks in advance.
I hate to work in code behind but sometimes that is only way. This can be done in two ways according to me:
Use EventAggregator to raise the event from your ViewModel once you are done with your filtering logic in it. Listen to this event in view code behind and in the handler do firstCombo.Focus()
Second is bit dirty, So there must be the button or the last control after which you apply filter on your listview. In the buttonClickHandler/or any event of the last control (like selectionchange) directly do firstCombo.Focus(). In this case the moment you will press your button focus will move to combo.
Thanks

How do you display a custom message box from the selectionchanged event of the silverlight datagrid?

I want to display a custom confirmation message box (ChildWindow) when the user selects a row in a DataGrid in Silverlight. The message box simply has 2 buttons, a yes and a no. When the user clicks No, I want to restore the previously selected item in the DataGrid. I have been able to accomplish all of that.
The problem is that when the message box appears and I click NO and I restore the previously selected item, the item that the user tried to select remains in the MouseOver visual state until I move the mouse over some other row.
Is there any known workaround for this unusual behaviour of the DataGrid, or is this perhaps a legitimate bug in the control? I have done my research and I have not found anything as yet.
Any help would be appreciated.
Thanks!
This is classic mouse enter/leave gotcha thats common in many areas of Silverlight and indeed in many other frameworks as well. The assumption is that that mouse in and out events will come in pairs but they don't when a something else hijacks the mouse events.
Thats is what is happening here the DataGridRow is simplisticly tracking mouse over using the standard mouse events. However when you show a child window while it is in the mouse over state not further mouse events go to the row. When you dismiss the childwindow the mouse is already outside the row so it still gets no events.
A possible workaround is to fiddle with the visual state of the row yourself before showing the child window:-
VisualStateManager.GotoState(someRow, "NormalSelected", false);
Not perfect but possible good enough.

Creating orderable list in WPF using ListBox (HOWTO or better approach?)

I've got a list of things that I want the user to easily edit the order of. Right now I'm binding them to a ListBox with a custom ItemTemplate. Within that template I have an UP & DOWN button. My goal was to move the item up/down based on the button clicked.
However I'm having trouble associating the button's click event with the actual item in the list. I've seen some folks setup a drag/drop for ordering items but that that would be too complex for this app (it's target user base =1, just me).
I assume this is possible with a ListBox. Maybe someone has a beter idea on how to implement this? It's not a huge set of data... less than 25 items.
The DataContext of the button (of the entire template, thus inherited by the button) is the item itself. Simply cast it to your desired type.

WPF: Stopping or reversing a selection change in a list box

Imagine this: you have a Master-Child window consisting of a list of items (the Master window) and a set of controls where you can edit the currently selected item (the Child Window). The child window has "Apply" and "Cancel" buttons.
A user begins editing values. He then changes the selection, before pressing the "Apply" button.
Your application displays a message "Apply changes to the current item?", with the buttons being "Yes", "No" and "Cancel".
If the user presses "Cancel" then the attempt at changing the current selection should fail.
The "CurrentSelection" item is databound.
I thought I could deal with this in the "setter" part of the CurrentSelection property. If the user selects "Cancel", I simply keep the CurrentSelection item as it is, and fire a PropertyChanged notification event to tell the form to update back to the old selected item. The control is ignoring this notification event. (Which makes sense, the Control is saying "I know the current selection has changed. I just changed it!")
Any ideas how to fix this? In summary, the control tries to change the bound SelectedItem, and I want to tell it "No you can't change this selected item right now".
And to do what Kent Boogaart pointed out, refer to this reply..
How to stop a WPF binding from ignoring the PropertyChanged event that it caused?
This sounds like you are looking for the memento pattern:
http://en.wikipedia.org/wiki/Memento_pattern
Hope this helps.
Just a thought without having tested it: try raising the property changed event in a separate message. The list probably has a latch to ignore any notifications whilst it's changing the property value. If you dispatch a separate message that contains the notification, the latch should be reset and it should handle it.

Resources