tricky binding question

3 posts, 1 answers
  1. Randy Hompesch
    Randy Hompesch avatar
    148 posts
    Member since:
    Nov 2008

    Posted 23 Nov 2016 Link to this post


    Below is an excerpt of some xaml I am using. The important thing to notice is that I have a listbox of checkboxes associated with each grid row.
    How do I bind the all the checkboxes that I want checked for selected gridview item?

    Thanks ... Ed



    01.<telerik:RadGridView  x:Name="ssSamples" Grid.Row="0" Grid.Column="0"
    02.                    AutoGenerateColumns="False"  IsReadOnly="True"
    03.                     ItemsSource="{Binding Samples}"
    04.                     SelectedItem="{Binding SelectedRow}"
    05.                     MaxHeight="500" RowIndicatorVisibility="Visible"
    06.                             >
    07.    <telerik:RadGridView.Columns>
    08.        <telerik:GridViewToggleRowDetailsColumn />
    09.        <telerik:GridViewDataColumn Width="auto" Header="Sample Key" DataMemberBinding="{Binding Sample_Key}"/>
    10.        <telerik:GridViewDataColumn Width="auto" Header="Client Sample Id" DataMemberBinding="{Binding ClientSampleID}"/>
    11.        <telerik:GridViewDataColumn Width="auto" Header="Arrival" DataMemberBinding="{Binding Arrival}"/>
    12.        <telerik:GridViewDataColumn Width="*" Header="Status" DataMemberBinding="{Binding Status}"/>
    13.    </telerik:RadGridView.Columns>
    14.    <telerik:RadGridView.RowDetailsTemplate>
    15.        <DataTemplate>
    16.            <Grid Width="Auto" HorizontalAlignment="Stretch">
    17.                <Grid.RowDefinitions>
    18.                    <RowDefinition />
    19.                    <RowDefinition />
    20.                </Grid.RowDefinitions>
    21.                <Grid.ColumnDefinitions>
    22.                    <ColumnDefinition Width="Auto" />
    23.                    <ColumnDefinition Width="Auto" />
    24.                    <ColumnDefinition Width="Auto" />
    25.                    <ColumnDefinition Width="Auto" />
    26.                </Grid.ColumnDefinitions>
    28.                <Label Content="Associated Techniques:" Grid.Row="0" Grid.Column="2"
    29.                       HorizontalAlignment="Left" HorizontalContentAlignment="Left"  Width="350" Height="auto"/>
    30.                <telerik:RadListBox x:Name="lstTechniques" Grid.Row="1" Grid.Column="2" MaxHeight="210"
    31.                    SelectionMode="Extended"
    32.                    ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,
    33.                                    AncestorType={x:Type UserControl} },
    34.                                    Path=DataContext.Techniques}"
    35.                                        >
    36.                    <telerik:RadListBox.ItemTemplate>
    37.                        <DataTemplate >
    38.                            <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"
    39.                                                        Content="{Binding TechniqueName}"/>
    40.                        </DataTemplate>
    41.                    </telerik:RadListBox.ItemTemplate>
    42.                </telerik:RadListBox>
    43.            </Grid>
    45.        </DataTemplate>
    46.    </telerik:RadGridView.RowDetailsTemplate>
  2. Answer
    Dilyan Traykov
    Dilyan Traykov avatar
    1173 posts

    Posted 24 Nov 2016 Link to this post


    The DataContext of each RowDetails element is its parent row's bound data item. Thus, provided the objects inside your Samples collection have a Techniques collection property, where each Technique has its own IsSelected and TechniqueName properties, the following code should result in the expected result.

    <telerik:RadListBox x:Name="lstTechniques" Grid.Row="1" Grid.Column="2" MaxHeight="210"
    ItemsSource="{Binding Techniques}">
            <DataTemplate >
                <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"
                                      Content="{Binding TechniqueName}"/>

    If you, however, wish to bind your RadListBox's ItemsSource to a Techniques property in your view model, you will need to specify the Source of this binding, similarly to:

    <telerik:RadListBox x:Name="lstTechniques" Grid.Row="1" Grid.Column="2" MaxHeight="210"
                        ItemsSource="{Binding Techniques, Source={StaticResource MyViewModel}}">
            <DataTemplate >
                <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"
                  Content="{Binding TechniqueName}"/>

    For your reference, I'm attaching a sample project based on your scenario.

    I hope you find this information helpful. Please let me know if you need any further assistance on the matter.

    Dilyan Traykov
    Telerik by Progress
    Telerik UI for WPF is ready for Visual Studio 2017 RC! Learn more.
  3. Randy Hompesch
    Randy Hompesch avatar
    148 posts
    Member since:
    Nov 2008

    Posted 26 Nov 2016 in reply to Dilyan Traykov Link to this post

    Got it! Thanks.


Back to Top