This is a migrated thread and some comments may be shown as answers.

Binding header text

12 Answers 346 Views
GridView
This is a migrated thread and some comments may be shown as answers.
IT Development
Top achievements
Rank 1
IT Development asked on 05 Jul 2010, 10:23 AM

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

Sort by
0
Pavel Pavlov
Telerik team
answered on 05 Jul 2010, 11:48 AM
Hello Tim,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Alexey Oyun
Top achievements
Rank 1
answered on 10 Sep 2010, 07:21 AM
Hi,
Could you give small example of bindning object to header, please.
Thanks, Alex.
0
Pavel Pavlov
Telerik team
answered on 13 Sep 2010, 02:56 PM
Hi Alexey Oyun,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Alexey Oyun
Top achievements
Rank 1
answered on 13 Sep 2010, 03:14 PM
Thank you, I thought I would have DataContext of Grid. Anyway your solution through resources my workout for me.
0
Richard Koslik
Top achievements
Rank 1
answered on 15 Oct 2010, 12:39 PM
Hello!

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
0
Pavel Pavlov
Telerik team
answered on 15 Oct 2010, 02:57 PM
Hello Richard Koslik,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Richard Koslik
Top achievements
Rank 1
answered on 18 Oct 2010, 07:58 AM
Hello!

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
0
Pavel Pavlov
Telerik team
answered on 20 Oct 2010, 12:39 PM
Hello Richard Koslik,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Stephen
Top achievements
Rank 1
answered on 10 Dec 2010, 07:05 AM
Hi,

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  Width="Auto" MinWidth="110" IsReadOnly="True">
                                                <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>
<telerikG:GridViewDataColumn  Width="Auto" MinWidth="110" IsReadOnly="True">
                                                <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>
<telerikG:GridViewDataColumn  Width="Auto" MinWidth="110" IsReadOnly="True">
                                                <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>
<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>
<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>
<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>
0
Dipti
Top achievements
Rank 1
answered on 02 Aug 2011, 12:41 PM
Hi Pavel,

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.
0
Vanya Pavlova
Telerik team
answered on 02 Aug 2011, 12:53 PM
Hi Dipti,

 

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!


Kind regards,
Vanya Pavlova
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Deborah
Top achievements
Rank 1
answered on 30 Aug 2011, 02:52 PM
Thank you very very much Pavel,  your sample project has just solved my problem.
Tags
GridView
Asked by
IT Development
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Alexey Oyun
Top achievements
Rank 1
Richard Koslik
Top achievements
Rank 1
Stephen
Top achievements
Rank 1
Dipti
Top achievements
Rank 1
Vanya Pavlova
Telerik team
Deborah
Top achievements
Rank 1
Share this question
or