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!
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:
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?
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 am creating the TextColumn at runtime and adding it to the Grid from ViewModel. I want add HeaderTemplate to TextColumn. I want to add one Button and one TextBlock to headerTemplate at runtime and for the Button I want to add an event. I am able to add the button but if i add Click Event its throwing an exception saying we can not add Event to it. I
tried one more way of doing this. In the Xaml i created a DataTemplate with Button and TextBlock. and I am adding this datatemplate to Grid TextColumn HeaderTemplate. But before adding to TextColumn HeaderTemplate I want to change the TextBlock Text, I want to display the Column Name for that TextBlock. I am finding the Textblock control from the codebehind and changing its Text. But in UI its not updating the Text. Can you please help me in this.
What I want is adding the HeaderTemplate to TextColumn from codebehind and that HeaderTemplate will contain one Image Button and one textBlock. Button is to open a new popupwindow and TextBlock is to display column header.
Check if the attached solution works for you. It uses a DataTemplate added to page's resources and the column is generated in code-behind in page's Loaded event.
Hope this helps,
Hi Georgi,
Thank you for your response. Using resource file I am able to add button event but here the problem is I want to change my TextBlock Text which is in the Resource file along with Button. I am not able to change the Text. I want to give the Column Name to textBlock Text.
Thanks
The attached sample shows exactly this. Basically, you'd want to bind the TextBlock text to your column's key (the name of your column).
This is done by just binding the TextBlock to the DataContext:
I'm not sure what are you trying to achieve , could you describe your scenario in more detail ?
It'll be nice if you could illustrate what you want your grid to look like (for example, with a screenshot or a quick sketch) as well as your data object model.
As I'm not quite sure what you're trying to achieve, it's hard to tell you what's the best way to achieve it.
Regards,
You told to bind the TextBlock text to column's key . I dont want to bind the Column's key instead I want to bind someother property of the Grid ItemsSource underlying object. Why because now in the Converter class i am just getting the TextColumn key value as TextBlock text, instead i want to get the full Object, say my I have object called Emplyee and it has property ID, Name and Description. Now TextColumn key value I am passing as ID of the Emplyee class. But in converter class i want to access the Name property. Is this possible ?
Its really great to get response so quickly. Thank you so much for the solution, This works fine.
Thank You
One possible way to achieve this is to use a converter and modify the HeaderText to fit your needs in the Convert method. Attached is an updated sample that shows this.
Thank you for your kind response. What you said is right. But the problem is I am creating the TextColumn from codebehind and Loading the Grid. I dont want to display the keyname as HeaderText. I want to specify a customized text as HeaderText. Why because my key may contain underscore so I want to change the Text, Is it possible If I give a TextColumn HeaderText then this datatemplate TextBlock text should take the TextColumn HeaderText.