Now I'm about to learn RoutedEvents. I understand bubbling and tunneling and all. But I have a little problem I'm not sure how to handle.
I've got a ItemsControl in my own defined UserControl ( inherits TabItem ). This itemsControl could be a ListBox or some selector not decided yet ( depends what is best with this solution ).
Now when one item there is selected ( Not sure yet what the best selection method would be, perhaps doubleclick ) I need to capture the event in the TabControl that holds my UserControl. Thankfully as my userControl is put into the XAML visualtree the bubbling event flows up and events can reach the TabControl. However I still have a few problems with implementing this.
1) Let's say I set it as MouseDoubleClick I don't wan't other DoubleClicks ( like in other tabs or outside the ItemsControlItems ). I guess you can always do a check where the events coming from to ensure it's the right one but I don't feel it's the right way and it could be asking for trouble.
2) If I set the event to SelectionChanged and the ItemsControl to ListBox I would have to ensure the ListBox itself doesn't handle it so it can reach the tabControl. How would I do that?
3) Same as 2) but the TabControl is a selector itself so it would raise it's own selectionchange events wouldn't it. how would that work. Would the handler grab both from the listbox and the TabControl?
4) Not sure what ItemsControl I should use. If i'm only using it to doubleclick the item and then handle the event wouldn't ListBox be too much. It has many effects that would be undisireable like selection and such. Is it better to define your own ItemsControl or maybe use ListBox and overwrite some of it's properties like selection ( how would one do that? )?
5) The source of the event would be the ItemsContainer but I wan't the item itself. There is a easy way to get access to it isn't there?
6) Let's say I put a button and in the DataTemplate I wan't to attach the ID of the object through the button and capture it where the button click is handled to identify what item it was. Is there a property do do that or would I have to define it myself?
7) Would the best course of action here maybe to create my own event?
Your take on this issue and comments on what process is best to take here is appriciated.
EDIT: Decided to add little bit more info as asked.
What I'm aiming for; when I start the program I have a TabControl with 1 tab in it. This tab is different from the rest of the tabs will be because it holds a list of objects (CompanyModel). This list is the ItemsControl I mention. When you press one company from this list a new tab is added with info about that company.
My solution was to create a TabItem collection as a source for the TabControl. Then I would define my 2 types of TabItems in a seperate XAML source file (not sure what to call it, basicly add new usercontrol). One is the initial where thhe list appears, the other one takes in a CompanyModel as a constructor parameter and shows info about that.
This works while I don't like the idea of defining the company list tabitem seperatly I can't define it under the tabcontrol because I can only have defined items or sourced, not both.
I've tried this idea by putting a button on the List DataTemplate that bubbles the Id of the company selected up to the TabControl, handling it there and adding a new TabItem with the CompanyModel. That was just a solution to try it and has problems like if I add anpther button to the CompanyTabs I would grab them aswell in the TabControl.
So my problem could be solved by finding a good way to bubble a event from the original listTab or If I would be able to define the tab in the same place as the TabControl.
I know that one other solution would to have the list seperate from the TabControl but others would like to see how this comes out and I would like to learn how I would do this.
please have a look in this blog, may be it will helps u.
http://blogs.u2u.be/diederik/post/2009/09/09/In-WPF-SelectionChanged-does-not-mean-that-the-selection-changed.aspx
Related
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
I am trying to work though the best approach to accomplish the following. Within a page I have it divided into two section. On the left a listbox and the right is empty. (Grid etc). What I would like to accomplish is when an item is selected from the listbox a different user control loads in the right panel. For example if I have three items (one, two three) selecting one would load a red user control, two would load a blue user control and three a green user control.
I was taking this approach since Content Template / Data template selectors are not available in SL. However if anyone has another suggestion I would be grateful for your thoughts.
I'm creating this with MVVM in mind and traditionally I have managed this within the code behind of the user control however I have seen mention of how this could be managed within the ViewModel as well.
Any suggestions or guidance on a best approach is always appreciated.
Cheers
You can bind both listbox selected item and user control visibility properties to the same property in the viewModel.
Then just use a valueConverter for each user control to switch on/off the visibility.
Please tell me if i should elaborate/add a code sample.
I have a bunch of different objects that are commonly edited in the same TabControl using different DataTemplates, but I want each DataTemplate to have a common look and feel with Ok and Cancel buttons at the bottom right of each tab that will close the tab or save the content and then close the currently selected tab. What's the best way to place buttons on each tab ? Is there a way to do it without copying and pasting the buttons and stack panel across all of my data templates ?
Sure, you can create your own OkCancelSaveControl. In WPF, creating a "user control" is much easier than it sounds. Here is a tutorial. In a nutshell, you
create a new user control,
create properties in the user control that give the your control the information it needs to perform its duties (e.g. the tab that it's supposed to close or the data object that it's supposed to save),
if necessary, create events that the user control raises (OkClick), in case some tab requires special treatment.
I would make a custom control, lets call it MyCoolTabItem, that inherits from the TabItem class, and just throw your buttons into the control. Then just add a MyCoolTabItem instead of a TabItem to all of your TabControls and it will have all of your buttons on it.
You could make a base view class that held those buttons. Views that needed the buttons would inherit them and common functionality.
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.
How can I define different ContextMenus for each column in the DataGrid (Microsoft's grid)? I found out that the grid provides a ContextMenu attribute, but i want different context menu items for the columns, not the same ContextMenu for the whole grid.
Edit: Thanks for your answer! I tried to listen to the ContextMenuOpeningEvent as you suggested which was a first success: the ContextMenu can be modified in the EventHandler. But it raises another (hopefully small) problem - I now have to identify the column the mouse cursor was over when the ContextMenuOpeningEvent was triggered. I'm going to research how to do (or work around) that later.
I haven't played with it at all, so this might be wrong, but you may be able to override the ContextMenuOpening event and create the appropriate menu on the fly.
add menu item to default context menu might give you a starting point.
Good luck.