Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1125
XamGrid Button in column purely through c#
posted

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.

 

Parents
  • 40030
    Verified Answer
    Offline posted

    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

     

     

     

Reply Children