Hello,
I would like to create two commands that will take as parameter a XamGrid and
1) Show the columnchooser for that grid
2) Export data to excel..
Those commands should be invoked by a context menu item or a simple button or a ribbon tool or whatever element is supported by the commanding framework.
I have tried to do so, but with no success...
Is it that difficult?
What am i missing?
Regards,
Michael
Hi Michael.
Here is a quick walkthrough on how to do this for the column chooser, excel exporting would be basically the same.
1. Implement ICommandTarget on your UserControl:
public partial class MainPage : UserControl, ICommandTarget
2. in the SupportsCommand method, return true for any command that you want to support:
bool ICommandTarget.SupportsCommand(ICommand command)
{
return command is OpenColumnChooserCommand;
}
3. in the GetParameter method, return what object you want to perform an action on, in this case the xamGrid:
object ICommandTarget.GetParameter(Infragistics.Controls.CommandSource source)
return grid;
4. Create a CommandSource class, this will be used to hook the event up to your button:
public class MyGridCommands : CommandSource
protected override ICommand ResolveCommand()
return new OpenColumnChooserCommand();
5. Implement the command
public class OpenColumnChooserCommand : CommandBase
public override bool CanExecute(object parameter)
if (parameter is XamGrid)
return true;
return false;
public override void Execute(object parameter)
XamGrid grid = parameter as XamGrid;
if (grid != null)
grid.ShowColumnChooser();
6. Finally, hook up your command to your button:
<Button Content="Column Chooser">
<ig:Commanding.Command>
<local:MyGridCommands EventName="Click" >
</local:MyGridCommands>
</ig:Commanding.Command>
</Button>
Hope this helps simplify things.
-SteveZ
Hi Steve,
Thanx for the walkthrough, i haven't tested yet but i think there is an issue...
How will i support two Grids on the same user control?
Thanx in Advance
Hi Michael,
Np. The sample i posted was just a basic case.
But you could pass in your View/Command Target as the Parameter.
return this;
Then on your command source, you can add a property that allows you to pass information about what cntrl you want to execute on/
And finally in the command, you can use that info to resolve the control.
MyGridCommands cs = this.CommandSource as MyGridCommands;
XamGrid grid = null;
if (cs != null)
if (cs.Param = "grid1")
grid = cs.grid1;
else if (cs.Parameter = "grid2")
grid = cs.grid2;
if(grid != null)
grid.ShowColumnChooser;
Hello Steve,
i implemented the attached sample...
It has a Grid and a context menu in it, a button and a xamGridEx (an inherited xamGrid that implements ICommandTarget and supports ColumnChooserCommand) with a context menu in it.
The context menus has an item with a column chooser command source.
Also the button has a column chooser command source.
The button works as expected, but the menu items do nothing....
Have i done something wrong?
I have an application that shows at least 15 grids with a Context Menu that will export the contents of the grids or will show the column chooser command....
What should i do in order to support this common scenario?
Regards
The problem with ContextMenu is that it doesn't actually live in the VisualTree, it lives in a popup outside of it.
What this means, is that it can't participate in normal bindings, that involve DataContext inheritance or ElementName bindings.
On top of that, it can't resolve it's CommandTarget either.
Check out this help topic on how the CommandTarget is resolved.
http://help.infragistics.com/NetAdvantage/Silverlight/2010.3/CLR4.0/?page=SL_xamGrid_About_Infragistics_Commanding.html
Your best bet is probably to use the TargetName property on your commandSOurce
Then register yourself with the CommandSourceManager:
public MainPage()
CommandSourceManager.RegisterCommandTarget(this);
InitializeComponent();
what i did was to inherit from XamGrid and support the two commands.
The only thing that you set on xaml is the targetname.
I also cannot bind target to grid when i use the command source on a button. Is that Normal?
Suggestion:
Please implement ICommandTarget on XamGrid in order to support these types of commands...
Also in the column Chooser window, it would be nice if you could select(navigate and edit) a nested column layout
PS
I attach a sample with the implementation. It also shows the problem with Target binding on a Button element...
Thanx
So Target is of type object, so setting a binding on it, is actually just making the Target property equal to a binding.
However, you can use the Attached CommandTarget property and put it on your button, like so:
<Button Content="Column Chooser" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="149" ig:Commanding.CommandTarget="{Binding ElementName=xamGrid1}">
<ig:ColumnChooserCommandSource EventName="Click" />
Hope this helps,
Thank you Steve.