Hi,
I saw this Xaml code in this forum:
<igGrid:XamWebGrid Name="xamWebGrid1"> <igGrid:XamWebGrid.Columns> <igGrid:TextColumn Key="Value1"> <igGrid:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="1Some looooong text that should be split in multiline " TextWrapping="Wrap" Width="120"/> </DataTemplate> </igGrid:TextColumn.HeaderTemplate> </igGrid:TextColumn> <igGrid:TextColumn Key="SomeLongPropertyNameThatWillBeWrappedInTheGridHeader"> <igGrid:TextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding}" TextWrapping="Wrap" Width="120"/> </DataTemplate> </igGrid:TextColumn.HeaderTemplate> </igGrid:TextColumn>
</igGrid:XamWebGrid.Columns> </igGrid:XamWebGrid>
I would like to convert to C# but I don't know how I can add/insert a datatemplate to hold the textblock.
xamWebGrid1.Columns.Add(new TextColumn() { Key = "Value1", HeaderText = "SomeLongPropertyNameThatWillBeWrappedInTheGridHeader", Width = new ColumnWidth(85, false), IsReadOnly = false, });
Can anyone help? Thanks!
It would be easier for you to go with the approach where you define your data template in a Resource Dictionary and then load it in C# code. For more info about how Resource Dictionaries works check out this link.
To put a button in it you just need to place a button in the xaml of the resource dictionary. However hooking a event handler to any of the button's events is not possible, because a DataTemplate does not contains the instances of its descendants. For more info on this topic take a look at this page or use google to research the topic.
Regards
I want to add a button to DataTemplate and i want event also for the Button, Can you please let me know how to do
The idea behind this code is that XamlReader.Load(string YourXamlCode) will parse the string you provide into run time object. XamlReader returns object and you need to cast that object to whatever you are trying to parse. In your case that is TextBlock. So basically your code should look something like this:
string path = "YourObject.Name" //this is example
string xaml = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> <TextBlock Text=""{Binding " + path + ", @"}""/> </DataTemplate>"; DataTemplate dt = (DataTemplate)XamlReader.Load(xaml);
HTH
Thanks for your reply Konstantin. I found this code in one of your links you have provided me:
private DataTemplate Create(Type type) { DataTemplate dt = new DataTemplate(); string xaml = @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"" xmlns:controls=""clr-namespace:" + type.Namespace + @";assembly=" + type.Namespace + @"""> <controls:" + type.Name + @"/></DataTemplate>"; return (DataTemplate)XamlReader.Load(xaml); }
If I like to create a Textblock, how/what do I need to pass to the Create function?
Hi tangolp,
You can set the HeaderTemplate for the TextColumn by using HeaderTemplate property of the TextColumn. So the code should look something like this:
amWebGrid1.Columns.Add(new TextColumn() { Key = "Value1", HeaderText = "SomeLongPropertyNameThatWillBeWrappedInTheGridHeader", Width = new ColumnWidth(85, false), IsReadOnly = false, HeaderTemplate = YourDataTemplate });
For the data template you have few options: