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

Setting ScrollPositionIndicatorTemplate Binding in CodeBehind

1 Answer 75 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 18 Apr 2011, 11:34 PM
So I am trying to dynamically build a datagrid, and I want to be able to have eh scroll position indicator bind to a different column at different times.

How do I change which column the ScrollPositionIndicatorTemplate's ContentPresenter binds to in the code behind?

I tried the following (and numerous variants thereof!) to no avail:

Binding binding = new Binding("Data.FirstName");  
            this.RadGridView1.ApplyTemplate();  
            ScrollPositionIndicator indicator = this.RadGridView1.ChildrenOfType<ScrollPositionIndicator>().First();  
            indicator.ApplyTemplate();  
            ContentPresenter presenter = indicator.ChildrenOfType<ContentPresenter>().First();  
            presenter.SetBinding(ContentPresenter.ContentProperty, binding);

1 Answer, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 19 Apr 2011, 01:20 PM
Hello Adam,

 

In my opinion the better approach here is to define a single DataTemplate in a UserControl's Resource Collection and in code behind just change the ScrollPositionIndicatorTemplate of RadGridView as shown below:

MainPage.xaml:

<UserControl.Resources>
    <DataTemplate x:Key="dt">
        <TextBlock Text="{Binding EmployeeName}"/>
        </DataTemplate>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
        <telerik:RadGridView x:Name="radGridView" ScrollMode="Deferred" ItemsSource="{Binding Collection}" AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="ID" DataMemberBinding="{Binding ID}"/>
                    <telerik:GridViewDataColumn Header="EmployeeName" DataMemberBinding="{Binding EmployeeName}"/>
                        <telerik:GridViewDataColumn Header="CompanyName" DataMemberBinding="{Binding CompanyName}"/>
                            <telerik:GridViewDataColumn Header="?" DataMemberBinding="{Binding IsMarried}"/>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        <Button Content="Button" HorizontalAlignment="Left" Height="24" Margin="240,0,0,8" VerticalAlignment="Bottom" Width="72" Click="Button_Click"/>
    </Grid>
</UserControl>

MainPage.xaml.cs:

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
    // TODO: Add event handler implementation here.
    this.radGridView.ScrollPositionIndicatorTemplate = this.Resources["dt"] as DataTemplate;
}


Regards,
Vanya Pavlova
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
Tags
GridView
Asked by
Adam
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Share this question
or