Hello,
For localization purposes I need to bind the header text of the radgridview.
The button under the radgridview is showing the correct text.
Why is the header text of the first column not showing anything?
<StackPanel>
<telerik:RadGridView AutoGenerateColumns="False" x:Name="RadGridViewGebruikers" ItemsSource="{Binding Users}" ShowGroupPanel="False" IsReadOnly="True">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding UserName}" Width="200">
<telerik:GridViewDataColumn.Header>
<TextBlock Text="{Binding ResourceGridHeaderGebruiker}"></TextBlock>
</telerik:GridViewDataColumn.Header>
</telerik:GridViewDataColumn>
<telerik:GridViewDataColumn Header="{Binding ResourceGridHeaderIsIngelogd}" DataMemberBinding="{Binding IsOnline}" MinWidth="200" Width="*"/>
<telerik:GridViewDataColumn Header="{Binding ResourceGridHeaderDatumActief}" DataMemberBinding="{Binding LastActivityDate}" MinWidth="200"/>
</telerik:RadGridView.Columns>
<telerikNavigation:RadContextMenu.ContextMenu>
<telerikNavigation:RadContextMenu Template="{StaticResource CrudContextMenu}">
</telerikNavigation:RadContextMenu>
</telerikNavigation:RadContextMenu.ContextMenu>
</telerik:RadGridView>
<Button Content="{Binding ResourceGridHeaderGebruiker}" />
</StackPanel>
Kind regards,
Tim van Rooijen
12 Answers, 1 is accepted
The reason for the binding to fail is as follows :
The DataContext of the header cell is the header string rather than the DataContext of RadGridView.
To be able to bind you may need to expose the ResourceGridHeaderGebruiker property in a static resource .
Sincerely yours,
Pavel Pavlov
the Telerik team
Could you give small example of bindning object to header, please.
Thanks, Alex.
I have prepared a small sample. There is one column with a header bound to a property of a small class.
This approach may be extended to fit your scenario.
Please note that if you need to change the headers runtime , the class needs to implement the INotifyPropertyChanged interface. This way RadGridView update the header each time the property is changed.
Kind regards,
Pavel Pavlov
the Telerik team
I also want to change the header, but I need a converter for displaying my text. How does this work! My problem is he does'nt call the converter, it is always displayed the FALLBACKVALUE!
<
telerik:RadGridView
HorizontalAlignment
=
"Left"
Margin
=
"23,46,0,0"
Name
=
"grdDisc"
VerticalAlignment
=
"Top"
Height
=
"275"
Width
=
"240"
AutoGenerateColumns
=
"False"
ItemsSource
=
"{Binding tabMONDISC}"
SelectedItem
=
"{Binding SelItemMONDISC, Mode=TwoWay}"
IsBusy
=
"{Binding bIsLoading}"
IsReadOnly
=
"True"
RowIndicatorVisibility
=
"Collapsed"
>
<
telerik:RadGridView.Columns
>
<
telerik:GridViewDataColumn
DataMemberBinding
=
"{Binding MONDISC_S_LETTER}"
Width
=
"100"
Header="{Binding
Path
=
iActiveLanguage
,
FallbackValue
=
LaufwerkFB
, Converter={StaticResource
localLngConverter},
ConverterParameter
=
4121
}" />
</
telerik:RadGridView.Columns
>
</
telerik:RadGridView
>
iActiveLanguage => aktuelle Sprache zur richtigen Darstellung der Texte
FallbackValue => default value for development-modus
localLngConverter => Converter for getting the right text
ConverterParamter => number of the text
Why is the FALLBACKVALUE displayed and not the converter called??
Best regards,
Richard
Which is the object that exposes the iActiveLanguage property ?
I believe the reason for the binding to fail is that the object owning the iActiveLanguage property is not a DataContext for the header cells.
Please let me know where this object is instantiated so I could help you further .
Regards,
Pavel Pavlov
the Telerik team
The property iActiveLanguage is not used for the converter. We only used it, because we needed a binding variable and it is created in the VieModel. For us important are converter, converterparameter and FallbackValue, so that we are able to get the right text displayed.
I tried it with the static resource and it worked, but is there a simplier way so that I don't need an additional property?
Best regards, Richard
I believe exposing the source which you are binding to as a static resource and using the "{Binding .., Source = {StaticResource .... " syntax is the simplest solution.
Regards,
Pavel Pavlov
the Telerik team
The above approach of using a static resource to connect to the class does not work for me, I think because I do no use a default constructor. The error i get is 'No matching constructor found on type 'myViewModel'. My viewModel's constructor takes in some interfaces as parameters. If I put in a default constructor in, I dont get the error, but my column headers are empty, I assume because it has created a new viewModle with the default constructor and not using my dataContext which has the correct column heading.
An alternative way of doing this which I think should work is this: But it too fails to print anything in the header.
<
telerikG:GridViewDataColumn.HeaderCellStyle
>
<
Style
TargetType
=
"telerikG:GridViewHeaderCell"
>
<
Setter
Property
=
"Template"
>
<
Setter.Value
>
<
ControlTemplate
TargetType
=
"telerikG:GridViewHeaderCell"
> <
ContentPresenter
>
<
StackPanel
Orientation
=
"Horizontal"
> <
TextBlock
Text
=
"{Binding ErrorHeading, TargetNullValue=Errors}"
/>
</
StackPanel
> </
ContentPresenter
>
</
ControlTemplate
>
</
Setter.Value
>
</
Setter
>
</
Style
>
</
telerikG:GridViewDataColumn.HeaderCellStyle
>
In your attached code below is code statement,
<UserControl.Resources>
<local:Headers x:Key="Headers" />
</UserControl.Resources>
Suppose my Headers class is parameter less then it will give me error. Could you please suggest any other way. As i am trying to bind source class as my PresentationModel class.
Generally you do not have to predefine the template of GridViewHeaderCell to achieve the desired result. In this way you will loose its default appearance and capabilities. The better option is to predefine its Header property. Furthermore I believe that the following one would be useful for you on that matter "GridViewCellHeader style unable to see datacontext".
Hope this helps!
Vanya Pavlova
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>