I am creating an xamgrid in c#, there's no xaml at all. How can I add a button to a column purely through c#? Or even hyperlinked text if this is easier? Please be explicit about every small detail as I'm new to this!
Basically I am creating multiple grids with dynamic columns based on a set of configuration data so I have done everything in c# to create/build/bind grid. I have seen examples for creating data templates in xaml with button controsl within them, but cannot work out how to do this from c#.
Thanks.
Hi,
Basically you'd have to use a xamlReader to read in your DataTempate:
http://stackoverflow.com/questions/59451/creating-a-silverlight-datatemplate-in-code
As for actually executing code on the Button, i'd recommend using a custom RowCommand
public class MyCommandSource : CommandSource
{
protected override ICommand ResolveCommand()
return new MyCommand();
}
public class MyCommand : RowCommandBase
protected override void ExecuteCommand(Row row)
//YourData obj = (YourData)row.Data;
// perform action.
TemplateColumn tc = new TemplateColumn();
tc.Key = "Title";
tc.ItemTemplate = this.GetTemplate();
grid.Columns.Add(tc);
public DataTemplate GetTemplate()
string template = @"<DataTemplate
xmlns=""http://schemas.microsoft.com/client/2007""
xmlns:ig=""http://schemas.infragistics.com/xaml""
xmlns:local=""clr-namespace:XamGridApp_10._3;assembly=XamGridApp_10.3"">
<Button Content=""Click Me"">
<ig:Commanding.Command>
<local:MyCommandSource EventName=""Click"" ></local:MyCommandSource>
</ig:Commanding.Command>
</Button>
</DataTemplate>";
return XamlReader.Load(template) as DataTemplate;
Hope this helps,
-SteveZ
Thanks, where should the local namespace and assembly point to?
I figure it shoul point where I put the MyCommandSource, which I placed within the code behind but can't get it to work using my code behind's namespace.
Here's the error I am getting:
The type 'MyCommandSource' was not found because 'clr-namespace:XamGridApp_10._3;assembly=XamGridApp_10.3' is an unknown namespace. [Line: 13 Position: 70]
OK, so found out my assembly from project settings - newbie mistake ;)
I am getting an error now due when binding my grid - Is there a way I can turn off binding for a column? If I give the column a key that exists in datasource then the button appears.
OK, I can create an UnboundColumn instead of a template column which does the trick - hope this helps anyone else going through this for the first time.
Thanks for your initial post.
How can I get the button that was clicked, I am placing multiple columns in containing buttons? Thanks
Here's an update: I am still unsure how to get the button which was clicked, I have tried ColumnCommandBase instead of your example which uses RowCommandBase but didn't have any luck with that approach (Debugged into it and the ExecuteCommand override method didn't get focus at all).
When we have a way to know which button was clicked I need to then open a dialog. It would be nice if I could run a method in my code behind - is this possible from the separate class which contains ExecuteCommand? I can trigger the functionality I'm after from a button outside the grid, all I need is a way to work out which button was clicked and to then trigger my method in the silverlight page's code behind to open a dialog passing through the selected row.
Thanks for your assistance.
Solved :-)
I use the Button.Click property when I create the button and set the tag as follows
Tag="{Binding}"
I can catch the value of the column defined by the Key.
Hope this helps anyone looking for a similar solution,
Mike
Hi Everyone,
I am looking to do a similar action. I was able to do it in XAML. I am having problems catching the event handler. Where is it supposed to fire from? Is there a delegate/event I need to set?
Thanks!
OK, thanks Steve
ColumnCommandBase is actually only supported on the HeaderRow level, not the DataRow level, which is why you have to use RowCommandBase.
If you want to use the same command, you can always pass in a flag to the parameter of the commandSource telling it where you are.
Yes that would work, but I would rather use ColumnCommandBase instead of RowCommandBase- is that possible?