WPF DataGrid ContextMenu(s) - wpf

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.

Related

WPF Listview touch issue on window 10: "an event of another datatemplate is triggered while it may not happen"

When using a WPF application on windows 10 with a touchscreen we encounter an issue with the listview. When working with a mouse it works fine.
We have created a simple test-project, which is used to simulate the problem on windows 10 and can be found on GitHub. A ticket is also created on MSDN
In short below a summary of the technical setup:
We use a grouped listview, and for each group, a togglebutton and
another (inner) listview.
The inner listview uses an ItemTemplateSelector binded to an
Datatemplate selector, to choose an datatemplate.
There are 3 datatemplates (checkbox, numeric, text) that will be
choosen based upon the type property of the bounded model.
Each datatemplate has a stackpanel. The stackpanel in the text en
numeric datatemplate is wired to an PreviewMouseDown event.
Important, the stackpanel in the checkbox isn't wired to an event.
This works in general well, however sometimes, when touching the checkbox, the
PreviousMouseDown of another template is triggered.
I would expect that this behavior may not happen, is that correct?
We found a workaround (*) for this issue but we didn't find the root
cause.
Why is the event of another template triggered?
I'm starting to believe that this could be an issue with WPF Listview
and touch behavior?
(*) If we know that sometimes an event is triggered from a wrong template,
we verify every event whether that event is originated from the right template and if not we do nothing.
Below you can see when tapping quickly on the checkbox the clickevent get's triggered.
Below more details of the code:
The datatemplates and the selector
The grouped listview with the inner listview and the itemtemplate selector
Below the code behind and the handler for the PreviewMouseDown event
Below an overview of the steps we have been taken in order to resolve
it, but none lead into a solution.
Since WPF is supporting touch and a touch on a screen is also "translated" into a mousedown event, I don't see a problem why not using a previewMouseDown event on a touch screen. I also didn't find any offical documentation of Microsoft in not doing this.
Anyway, I could no longer reproduce the problem that a touch on listviewitem is invoking another previeuwMouseDown event of a template of another listviewitem in the list by ....
Changing the PrevieuwMouseDown event by a Touchdown event!
I am glad to find a solution, however based upon many online searches, I feel there are many issues with thouch on WPF and often it's is not clear what the rootcause is. Like in this case I found a solution by trial and error, but why the problem occurs when using previewMouseDown, is puzzling.

WPF ListView arrow navigation and keystroke problem

I have a readonly WPF listView and I am having two problem with it
(1) When I load the listView, I set the selecteditem in code behind. It works fine but when I use up/dowm arrow key to navigate through the list, it always jump to the first item at start.
(2) I can't use keystroke to select item
Can anyone give me some idea about how to solve them?
Thanks
Yes, the problem is you need to call Focus() on the list item after selecting it.
But for various reasons it's not as simple as all that.
See Arrow keys don't work after programmatically setting ListView.SelectedItem
...for full details.

How to make popup grow up in silverlight?

I'm having a problem with combobox popup (not sure if it's a problem of combobox). First when i click it, dropdown popup opens correct - upwards(there are about 50 items in it, and combobox is located in the bottom of the page, if it goes downwards, there would be only 5 items visible). But then if i select any item, and them open it again - it will open downwards and put items in a scroll. So how can I fix this and force popup open in the desired direction?
Are you setting the "MaxDropDownHeight" at some point? By default it is infinity but maybe setting to hard # greater than # in combo box might help.
Also I just tested this in SL4/Firefox and the combo box pops up each time (with or without a selected item). What version of SL are you using?
Well, what I've done - I subscribed to SizeChanged event of the Border element which is a child of Popup.Child canvas. In that method, I'm calculating space below and above the combo box and then setting Top offset using Canvas.SetTop method to that border and it's MaxHeight. Maybe I will post some code later.
I was having this exact same issue (actually with Silverlight 5, but the same behavior nonetheless). I tried various things with MaxDropDownHeight but that did not correct the problem.
#Walker the approach you mentioned in your answer sounded promising but I was not able to tell from your description how you actually implemented that.
In any case, I found a rather unlikely solution. I discovered that if I populated my ComboBox with ComboBoxItems instead of a collection of custom objects, then the dropdown/selection/direction issue does not occur. I've posted an answer here that goes into a little more detail.

Silverlight: Individual visibility of a RowDetailsTemplate for each data row

I'm trying to achive displaying the RowDetailsTemplate of a Silverlight DataGrid depending on a bool Property, bound to a CheckBox Control's IsChecked Property. Insinde of my RowDetailsTemplate there is a single custom UserControl, containing further Controls.
Since the DataGrid only allows a global setting (RowDetailsVisibilityMode) Some code-behind is needed. I've implemented a solution based on Rorys Reply (and using a behaviour-technique) which actually works.
Unfortunately, The DataGrid doesn't remember the individually shown or hidden Rows on sorting. The checkbox remains selected, but the Row collapses. Further, no event like "OnAfterSort" or something similar seems to exist, where i could "Refresh" the visibility settings in a loop.
Another idea was to bind the Visibility of my custom Details-UserControl to the CheckBox bound value. This actually works (when settings RowDetailsVisibilityMode to "Visible"), but I'm not able to get rid of this weird behaviour: When the CheckBox is checked, the Detail Template expands and the detail UserControl appears. Nice. When the CheckBox is unchecked again, the UserControl disappears (Visibility is set to Collapsed) but the Row doesn't collapse and the blank space remains (as it would be set to Hidden not Collapsed).
Do you have any ideas?
I hope it's ok I didn't post any code samples, the implementation is pretty easy and I believe that the problem doesn't actually lie in a coding mistake i made. You can setup a simple DataGrid quickly like in this perfect MSDN Example. Starting from here, it's easy to test both described behaviours!
Really big thanks in advance,
- Thomas

WPF: Handling bubbling events only from one specific content

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

Resources