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

[Solved] GridViewCellHeader style unable to see datacontext

5 Answers 247 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Stephen
Top achievements
Rank 1
Stephen asked on 13 Dec 2010, 05:04 AM
Hi there,

I have read the threads in this forum about binding data to the header of a grid column, and the suggestion seems to be declaring your viewmodel class in the resources section of your xaml and referencing it as a StaticResource. I tried this and got errors, 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 viewModel with the default constructor and not using my DataContext which has the correct column heading.

When I tried to solve same thing with the normal Silverlight DataGrid, I extracted out the style of the DataGridColumnHeader in my resources section, and in the ControlTemplate, I put this code in and it worked:
<ContentPresenter>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock  Text="{Binding ErrorHeading, TargetNullValue=Errors}"/>
                                        <!--<Button Commands:Click.Command="{Binding DataContext.RefreshCommand, ElementName=LayoutRoot}" Margin="5,0,0,0" Width="20" Height="20" ToolTipService.ToolTip="Last Known value updated at: 22/5/10: 17:45:54">
                                            <Image Source="/Resources/Refresh.jpg"/>
                                        </Button>-->
                                    </StackPanel>
                                </ContentPresenter>
<ContentPresenter>
  <TextBlock Text="{Binding ErrorHeading}"/>
</ContentPresenter>

So I tried to do the same thing with the Telerik control, when I edited the style of the GridViewHeaderCell, It had this line which was slightly different to the one which worked above:
<ContentControl x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}"  Grid.Column="0" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsTabStop="{TemplateBinding IsTabStop}" Margin="{TemplateBinding Padding}" Style="{StaticResource StretchedContentControl}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                    <ContentControl.Content>
<TextBlock  Text="{Binding ErrorHeading, TargetNullValue=Errors}"/>
                    </ContentControl.Content>
                    <!---->
                </ContentControl>
<ContentControl x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}"  Content="{TemplateBinding Content}" Grid.Column="0" ForeGround="{TemplateBinding Foreground}" Style="{StaticResource StretchedContentControl}" />

I tried replacing the content with my binding like this:
<ContentControl .... etc >
   <ContentControl.Content>
<TextBlock Text="{Binding ErrorHeading}" />
   </ContentControl.Content>
</ContentControl>

But the binding still does not work. If I change the binding to plain text, it works. Any help would be much appreciated.

Thanks,
Stephen




5 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 13 Dec 2010, 11:59 AM
Hi Stephen,

 You don't need to edit the style of the header cell. You can place any UI element in the Header property of a GridViewColumn. Also, the alternative to declaring your ViewModel as a StaticResource is to use a DataContexrProxy.

Kind regards,
Yavor Georgiev
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Stephen
Top achievements
Rank 1
answered on 14 Dec 2010, 01:05 AM
Thanks for your reply - it is very close to working using the DataContextProxy. Basically I can access DataContext elements on the top level of my viewmodel now, but my ViewModel looks like this

public class ViewModel
{
private String _myString;
private ObservableCollection<TabItemViewModel> _tabItems;

public String MyString { blah }
public ObservableCollection<TabItemViewModel> TabItems {blah}
}

and it works for accessing the MyString using XAML like this:
<TextBlock  Text="{Binding Source={StaticResource DataContextProxy}, Path=DataSource.MyString}"/>

but I'm not sure how to get it to point at the ErrorHeading inside the observable collection of TabItemViewModels...

public class TabItemViewModel
{
private string _errorHeading;
 public string ErrorHeading
        {
            get { return _errorHeading; }
            set
            {
                _errorHeading = value;

                RaisePropertyChanged("ErrorHeading");
            }
        }
public string ErrorHeading
{
get {return _errorHeading;}
set
{
_errorHeading = value;
OnPropertyChanged("ErrorHeading");
}
}
}


I tried it like this:
<TextBlock  Text="{Binding Source={StaticResource DataContextProxy}, Path=DataSource.TabItems.ErrorHeading}"/>

but had no luck.

Any ideas?

Thanks,
Stephen


0
Yavor Georgiev
Telerik team
answered on 16 Dec 2010, 05:54 PM
Hello Stephen,

 You need to access the index of the item you want to bind to, like so: Path=DataSource.TabItems[0].ErrorHeading}".

All the best,
Yavor Georgiev
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
stephen
Top achievements
Rank 1
answered on 16 Dec 2010, 11:23 PM
Instead of hardcoding the tab as a number index, is there a way to use the current bound tab? My datagrid is inside a tab control which is bound like this:

<telerikNavigation:RadTabControl ItemsSource="{Binding Path=TabItems}" 


here is the full code:

 <telerikNavigation:RadTabControl ItemsSource="{Binding Path=TabItems}" telerik:StyleManager.Theme="Vista">
            <telerikNavigation:RadTabControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Header}" VerticalAlignment="Center"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </telerikNavigation:RadTabControl.ItemTemplate>
            
            <telerikNavigation:RadTabControl.ContentTemplate>
                <DataTemplate>
                    <DataUI:TabTemplateSelector Content="{Binding}">
                        <DataUI:TabTemplateSelector.GridTemplate>
                            <DataTemplate>
                                <Grid Background="#FFE5E5E5">
                                    <!--<data:DataGrid x:Name="FormatGrid" d:LayoutOverrides="Width" -->
                                   
                                  
                                    <telerikG:RadGridView ItemsSource="{Binding DataList}" ShowGroupPanel="False" AutoGenerateColumns="False" >
                                        <telerikG:RadGridView.Columns>
<telerikNavigation:RadTabControl ItemsSource="{Binding Path=TabItems}" telerik:StyleManager.Theme="Vista">
            <telerikNavigation:RadTabControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Header}" VerticalAlignment="Center"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </telerikNavigation:RadTabControl.ItemTemplate>
             
            <telerikNavigation:RadTabControl.ContentTemplate>
                <DataTemplate>
                    <DataUI:TabTemplateSelector Content="{Binding}">
                        <DataUI:TabTemplateSelector.GridTemplate>
                            <DataTemplate>
                                <Grid Background="#FFE5E5E5">
                                    
                                    
                                   
                                    <telerikG:RadGridView ItemsSource="{Binding DataList}" ShowGroupPanel="False" AutoGenerateColumns="False" >
                                        <telerikG:RadGridView.Columns>
<telerikG:GridViewDataColumn Width="Auto" MinWidth="110" >                                                <telerikG:GridViewDataColumn.Header>
    <StackPanel Orientation="Horizontal">
        <TextBlock  Text="{Binding Source={StaticResource DataContextProxy}, Path=DataSource.TabItems.ErrorHeading}"/>
     </StackPanel>
  </telerikG:GridViewDataColumn.Header>
   </telerikG:GridViewDataColumn >
0
Tina Stancheva
Telerik team
answered on 22 Dec 2010, 02:55 PM
Hi Stephen,

Please accept my apology for the delayed response. I prepared a sample project illustrating a possible approach towards your scenario. Please have a look at it and let me know if it works for you or if you need more info.

Regards,
Tina Stancheva
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
Tags
GridView
Asked by
Stephen
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Stephen
Top achievements
Rank 1
stephen
Top achievements
Rank 1
Tina Stancheva
Telerik team
Share this question
or