hi ,
In our Silverlight application we are dynamically showing database results in the Xamgrid.Sometime we will dynamically add the columns or we will show Autogenerate columns based on our database results.We had kept column width as Auto. Our Database results are having longer column names, but the value will be two digit number. So it is occupying the more space unnecessarily.So we want to wrap the column Names to next line.
Could you please let me know how can I achieve this?
Hi,
Assuming the header text is just the key of the column (which it would be in an AutoGenerate situation),
Then you can just use the ColumnsHeaderTemplate property of the xamGrid to set a custom DataTemplate. In there you can put a TextBlock with TextWrapping on:
It should look something like this: <TextBlock TextWrapping="Wrap" Text="{Binding}"/>
Hope this helps,
-SteveZ
Hi SteveZ,Even in the case of Auto generated columns whether I need to create custom datatemplate? Because my application is completely dynamic so I am binding the Xamgrid with Dictionary<string, object> collection. Just for column header wrap it creates a big change.Please let me know the solution to resolve.
Thanks
Anil Kumar
Hi Anil,
It's actually just one property you need to set. And it would be applied to all columns including your AutoGenerated columns.
The other option is to create a new style targeted at HeaderCellControl and Re-template it, so that instead of using a ContentPresenter it uses a TextBlock with TextWrapping. Of course this change is a lot more massive, as you'd have to import all of the brushes that are used.
Hello,
I am just checking the progress of this issue and was wondering if you managed to achieve your goal or if you need any further assistance on the matter.
If the above suggestion helped you solve your issue please verify the thread as answered so other users may take better advantage of it.
Hello Anil,
What Stevez explained can be done through code by setting the XamGrid’s ColumnsHeaderTemplate property. Since in Silverlight you cannot create DataTemplate directly through code I suggest you predefine on in xaml like so:
<UserControl x:Class="Grid_Filtering.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ...>
<UserControl.Resources>
<DataTemplate x:Key="MyTemplate" >
<TextBlock Text="{Binding }" Foreground="Red" />
</DataTemplate>
</UserControl.Resources>
...
and load it through code like so:
xamGrid1.ColumnsHeaderTemplate = this.Resources["MyTemplate"] as DataTemplate;
If you need to have the DataTemplate created in code you can use the Silverlight XamlReader class and use its Load method to convert a xaml like string to a DataTemplate like so:
DataTemplate mytemplate =
XamlReader.Load("<DataTemplate x:Key='MyTemplate' " +
" xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
" xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' > " +
" <TextBlock Text='{Binding }' Foreground='Red' /> " +
" </DataTemplate>") as DataTemplate;
xamGrid1.ColumnsHeaderTemplate = mytemplate;
Please let us know if you require any further assistance on the matter.
Hi Stevez,
In Auto Generated column, I didn't get which property I need to set for the Xamgrid.
Could you please name that property and where exactly I need to set.
Thanks a lot for your Quick reply..