I have a control template defined to create a customized column header. In the template I have a context menu and am trying to pass back the target column as a command parameter. I have the actual command binding working and the following passes back the xamDataGrid, but I would like to pass back the index or ideally the bound Field object for that column. When the command fires, I need to know which column the user right clicked on. Open to other/better ways of doing it as well!
<ControlTemplate x:Key="Header1"> <Grid VerticalAlignment="Bottom"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="2"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Label Grid.Row="0" Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}"> <Label.ContextMenu> <ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}"> <MenuItem Header="Rename" Command="{Binding DataContext.RenameCommand}" CommandParameter="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/> </ContextMenu> </Label.ContextMenu></Label> <Border BorderThickness="1" Grid.Row="1" BorderBrush="LightGray"/> <Border BorderThickness="1" Grid.Row="2" Height="38"/> </Grid> </ControlTemplate>
Hello,
Thank you for contacting Infragistics. Since Field objects in the XamDataGrid are not part of any visual tree you cannot easily keep track which one was clicked on. You can write a style for LabelPresenter with an EventSetter using PreviewMouseRightButtonDown to know when the user right clicks.
eg.
<Style TargetType="{x:Type igDP:LabelPresenter}"> <EventSetter Event="PreviewMouseRightButtonDown" Handler="lp_PreviewMouseRightButtonDown"/> </Style> void lp_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { MessageBox.Show("LabelPresenter"); }
<Style TargetType="{x:Type igDP:LabelPresenter}">
<EventSetter Event="PreviewMouseRightButtonDown"
Handler="lp_PreviewMouseRightButtonDown"/>
</Style>
void lp_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("LabelPresenter");
}
However, returning the field as a command parameter would need to be done using a trigger, with the Path equal to SelectedItems.Fields[0]. This was previously discussed on the following forum post:
https://www.infragistics.com/community/forums/t/65082.aspx
Let me know if you have any questions.
Thanks for the suggestion, but it's not really what I'm trying to do. If I have a label in the header of the grid and it has a context menu that is bound to a command in a VM, is there any way to pass via a command parameter an object that would allow me to figure out what column the user right clicked on? Thank you.
Hello AE,
As I mentioned earlier the fields do not hold a selection per se. Therefore you would need to configure your application in such a way that clicking a label triggers some action in your view model that updates a property to accurately determine which field was clicked on. It is also much easier to determine the information about the column by using the mouse events on the grid. This was previously discussed here:
https://www.infragistics.com/community/forums/p/20190/244202.aspx#244202