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

DataContext of Telerik RadTabControl's Content Template

1 Answer 232 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
Julian
Top achievements
Rank 1
Julian asked on 12 Aug 2015, 10:35 PM
Hi everyone,

I'm having some trouble setting the data context property of my ContentTemplate within my RadTabControl (a tab control by Telerik). Here is my XAML:

    <telerik:RadTabControl Grid.Row="1" TabOrientation="Horizontal" ItemsSource="{Binding Counterparties}">
            <telerik:RadTabControl.ContentTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <!--<telerik:RadGridView Grid.Row="0" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ConsolidatedPositions}" Name="ExposureView" VerticalAlignment="Stretch" AutoGenerateColumns="False" CanUserDeleteRows="False" CanUserInsertRows="False" IsTabStop="False" RowHeight="18" SelectionMode="Extended" SelectionUnit="Mixed" IsReadOnly="True" ShowGroupPanel="False" ColumnWidth="75" GroupRenderMode="Flat" CanUserSortGroups="False" CanUserSortColumns="True" FontSize="11" RowIndicatorVisibility="Collapsed">-->
                        <telerik:RadGridView Grid.Row="0" ItemsSource="{Binding Path=DataContext.ConsolidatedPositions}" Name="ExposureView" VerticalAlignment="Stretch" AutoGenerateColumns="False" CanUserDeleteRows="False" CanUserInsertRows="False" IsTabStop="False" RowHeight="18" SelectionMode="Extended" SelectionUnit="Mixed" IsReadOnly="True" ShowGroupPanel="False" ColumnWidth="75" GroupRenderMode="Flat" CanUserSortGroups="False" CanUserSortColumns="True" FontSize="11" RowIndicatorVisibility="Collapsed">
                            <telerik:RadGridView.Columns>
                                <telerik:GridViewDataColumn Header="Cusip" DataMemberBinding="{Binding Path=Cusip, Mode=OneWay}"/>
                                <telerik:GridViewDataColumn Header="Description" DataMemberBinding="{Binding Path=Description, Mode=OneWay}" Width="150" />
                                <telerik:GridViewDataColumn Header="Par Net" DataMemberBinding="{Binding Path=ParNet, Mode=OneWay}" DataFormatString="N0" TextAlignment="Right" Width="100" CellStyleSelector="{StaticResource ParNetStyleSelector}"/>
                                <telerik:GridViewDataColumn Header="Dirty Price" DataMemberBinding="{Binding Path=Price, Mode=OneWay}" Width="80" DataFormatString="F3" TextAlignment="Right"/>
                                <telerik:GridViewDataColumn Header="Settled Position" DataMemberBinding="{Binding Path=SettledPosition, Mode=OneWay}" DataFormatString="N0" TextAlignment="Right" Width="105" CellStyleSelector="{StaticResource SettledPositionStyleSelector}"/>
                                <telerik:GridViewDataColumn Header="Repos/Reverse" DataMemberBinding="{Binding Path=Repos, Mode=OneWay}" DataFormatString="N0" TextAlignment="Right" Width="90" CellStyleSelector="{StaticResource ReposStyleSelector}" />
                                <telerik:GridViewDataColumn Header="Clean Market Net" DataMemberBinding="{Binding Path=CleanMarketNet, Mode=OneWay}" TextAlignment="Right" DataFormatString="N0" Width="120" CellStyleSelector="{StaticResource CleanMarketNetStyleSelector}" />
                                <telerik:GridViewDataColumn Header="Borrows/Lent" DataMemberBinding="{Binding Path=TotalBorrows, Mode=OneWay}" TextAlignment="Right" DataFormatString="N0" Width="100" CellStyleSelector="{StaticResource TotalBorrowStyleSelector}"/>
                                <telerik:GridViewDataColumn Header="Pledge In/Out" DataMemberBinding="{Binding Path=TotalPledges, Mode=OneWay}" Width="100" TextAlignment="Right" DataFormatString="N0" CellStyleSelector="{StaticResource TotalPledgesStyleSelector}"/>
                                <telerik:GridViewDataColumn Header="T+1 Net Pos" DataMemberBinding="{Binding Path=FutureNet1, Mode=OneWay}" Width="90" TextAlignment="Right" DataFormatString="N0" CellStyleSelector="{StaticResource FutureNet1StyleSelector}"/>
                                <telerik:GridViewDataColumn Header="T+2 Net Pos" DataMemberBinding="{Binding Path=FutureNet2, Mode=OneWay}" Width="90" TextAlignment="Right" DataFormatString="N0" CellStyleSelector="{StaticResource FutureNet2StyleSelector}"/>
                                <telerik:GridViewDataColumn Header="T+3 Net Pos" DataMemberBinding="{Binding Path=FutureNet3, Mode=OneWay}" Width="90" TextAlignment="Right" DataFormatString="N0" CellStyleSelector="{StaticResource FutureNet3StyleSelector}"/>
                            </telerik:RadGridView.Columns>
                            <telerik:RadGridView.InputBindings>
                                <KeyBinding Key="F2" Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ClearGridFiltersCommand}"></KeyBinding>
                                <!--<KeyBinding Key="F2" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ClearGridFiltersCommand}" CommandParameter="{Binding ElementName=ExposureView}"></KeyBinding>-->
                            </telerik:RadGridView.InputBindings>
                        </telerik:RadGridView>
                        <StatusBar Grid.Row="1">
                            <StatusBar.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="200" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="200" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="50" />
                                        </Grid.ColumnDefinitions>
                                    </Grid>
                                </ItemsPanelTemplate>
                            </StatusBar.ItemsPanel>
                            <StatusBarItem Grid.Column="0">
                                <Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Status}" Foreground="White"/>
                            </StatusBarItem>
                            <Separator Grid.Column="1" />
                            <StatusBarItem Grid.Column="2" >
                                <StackPanel Orientation="Horizontal">
                                    <Label Content="Last Update:" Foreground="White" />
                                    <Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.LastUpdate}" Foreground="White"/>
                                </StackPanel>
                            </StatusBarItem>
                            <Separator Grid.Column="3"/>
                            <StatusBarItem Grid.Column="4" HorizontalAlignment="Right">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                                    <!--<Label Content="NET Pledged: " Foreground="White"></Label>-->
                                    <!--<Label Content="{Binding NetPledged}" ContentStringFormat="{}{0:N0}" FontWeight="Bold" Foreground="{Binding Path=NetPledged, Converter={StaticResource RedNegativeLabelForegroundConverter}}"></Label>-->
                                    <Label Content="Market Value Borrowed: " Foreground="White"/>
                                    <Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.TotalParNet}" ContentStringFormat="{}{0:N0}" FontWeight="Bold" Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.TotalParNet, Converter={StaticResource RedNegativeLabelForegroundConverter}}"/>
                                    <Label Content="Market Value Pledged: " Foreground="White"/>
                                    <Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.TotalMarketValue}" ContentStringFormat="{}{0:N0}" FontWeight="Bold" Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.TotalMarketValue, Converter={StaticResource RedNegativeLabelForegroundConverter}}"/>
                                </StackPanel>
                            </StatusBarItem>
                            <Separator Grid.Column="5" />
                            <StatusBarItem Grid.Column="6" HorizontalAlignment="Right">
                                <Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.RecordCount}" Foreground="White"/>
                            </StatusBarItem>
                        </StatusBar>
                    </Grid>
                </DataTemplate>
            </telerik:RadTabControl.ContentTemplate>
        </telerik:RadTabControl>

I'm using MVVM Light. The Counterparties' data context is my VM called MainViewModel.  I want the ItemsSource of my GridView to be a different property of my ViewModel, called ConsolidatedPositions. So I want the RadGridView's DataContext to be the VM, **NOT** the collection of Counterparties, which it is currently doing. 

I've found a temporary solution, but it is SLOW:

    <!--<telerik:RadGridView Grid.Row="0" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}

My Windows DataContext:

    DataContext="{Binding Main, Source={StaticResource Locator}}"

Thanks for the help.

1 Answer, 1 is accepted

Sort by
0
Kiril Vandov
Telerik team
answered on 17 Aug 2015, 08:12 AM
Hello Julian,

The setting of the DataContext of the RadTabControl does not differ from the DataContext of other controls, here is an detailed explanation how the DataContext works:
When you have ItemsControl and you populate its children, you have a collection of data items in your ViewModel. Lets say you have List<DataItem> items = new List<DataItem>(), setting the data context to be the items property will automatically set the DataContext of each item to be the corresponding DataItem. The information that will be transferred to the children will be the one container in the DataItem, if you need to populate your TabItem with another collection it simply need to have it defined in the DataItem. You can find more information about the DataContext in this article.

I have also attached a sample project demonstating how you can bind the Header of each TabItem and populate its Template with ListBox (same applies for GridView).

I hope this information helps. Please let us know if you need further assistance.

Kind regards,
Kiril Vandov
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
TabControl
Asked by
Julian
Top achievements
Rank 1
Answers by
Kiril Vandov
Telerik team
Share this question
or