Expand panel and select item while using data binding

4 posts, 0 answers
  1. Øyvind Øyen
    Øyvind Øyen avatar
    6 posts
    Member since:
    Feb 2010

    Posted 16 Feb 2010 Link to this post

    Hi!

    After filling a RadPanelBar with data usin data binding, I would like to expand one of the panels and select one of the items in this panel based on an id of the item. Items contains my objects, so I can iterate through them and locate the item, but I cannot figure out how to expand the panel containing the value, neither set mye item selected(thus raise an event).

    xaml:
            <ScrollViewer Margin="0" Padding="0" VerticalScrollBarVisibility="Auto" BorderThickness="0" Grid.Row="1"
                <StackPanel Margin="0">              
                    <StackPanel.Resources> 
                        <core:HierarchicalDataTemplate x:Key="radServiceEngineerEntry"
                            <StackPanel Orientation="Horizontal"
                                <Forms:ServiceEngineerRepresenter DataContext="{Binding}" HorizontalAlignment="Left" Margin="0,0,0,0" MouseLeftButtonDown="ServiceEngineerRepresenter_MouseLeftButtonDown"/> 
                            </StackPanel>                        
                        </core:HierarchicalDataTemplate> 
                        <core:HierarchicalDataTemplate x:Key="radRotationEntry" ItemTemplate="{StaticResource radServiceEngineerEntry}" ItemsSource="{Binding ServiceEngineers}"
                            <StackPanel Orientation="Horizontal">                                         
                                <Forms:RotationRepresenter DataContext="{Binding}"/> 
                            </StackPanel> 
                        </core:HierarchicalDataTemplate> 
                    </StackPanel.Resources>          
                    <telerikNavigation:RadPanelBar BorderBrush="#9098a3" BorderThickness="1" Margin="5" x:Name="Rotations"  ItemTemplate="{StaticResource radRotationEntry}" />                          
                </StackPanel> 
            </ScrollViewer> 
    c#:
    Rotations.ItemsSource = rotations; 

    I woud Like to do something like this:
    foreach (RadPanelBarItem rotation in Rotations.Items) 
        rotation.IsExpanded = true

    But obviously rotation may not be casted to RadPanelBarItem as the items are my own businessobjects.

    I need help... :)

    /Øyvind


  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 17 Feb 2010 Link to this post

    Hi Øyvind,

    For this scenario you will have to use ContainerBindings. This link explains how to work with container bindings in RadTreeView, but the idea can easily be applied to RadPanelBar. Additionally, I have attached a sample project demonstrating the usage of container bindings in RadPanelBar. Have a look at it and let me know how it works for you.

    All the best,
    Kiril Stanoev
    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.
  3. DevCraft banner
  4. Øyvind Øyen
    Øyvind Øyen avatar
    6 posts
    Member since:
    Feb 2010

    Posted 17 Feb 2010 Link to this post

    Thanks for your replay!

    I do not quite understand the ContainerBinding IsExpanded, but i implemented it.

    I found a solution to my problem in this post:

    There were some problems as the items load after the panel is expanded. Until then the container is empty, thus there are yet no items to be selected. My solution to this was to check if the container is empty an listen to the container.Loaded event. If loaded, I listen to the container.Expanded event before selecting the item. The reason for this is that sender then wil be my newly expanded panel.

    Some code:
    private void SelectServiceEngineer() 
            { 
                foreach (Rotation rotation in Rotations.Items) 
                { 
                    var rotationContainer = Rotations.ItemContainerGenerator.ContainerFromItem(rotation) as RadPanelBarItem; 
     
                    if (rotationContainer != null && rotation.ServiceEngineers.Contains(_searchForServiceEngineer)) 
                    { 
                        if (rotationContainer.IsExpanded) 
                        { 
                            SelectServiceEngineerInPanel(rotationContainer); 
                        } 
                        else 
                        { 
                            if (rotationContainer.ItemContainerGenerator.ContainerFromIndex(0) == null
                            { 
                                rotationContainer.Loaded += RotationContainerLoaded; 
                            } 
                            else 
                            { 
                                rotationContainer.Expanded += RotationContainerExpanded; 
                            } 
                            rotationContainer.IsExpanded = true
                        } 
                        break
                    } 
                    if (rotationContainer != null
                    { 
                        rotationContainer.IsExpanded = false
                    } 
                } 
            } 
             
            private void SelectServiceEngineerInPanel(ItemsControl rotationContainer) 
            { 
                for (int i = 0; i < rotationContainer.Items.Count; i++) 
                { 
                    var serviceEngineerContainer = 
                        rotationContainer.ItemContainerGenerator.ContainerFromIndex(i) as RadPanelBarItem; 
                    if (serviceEngineerContainer != null
                        if (((ServiceEngineer) serviceEngineerContainer.Item).ServiceEngineerId == 
                            _searchForServiceEngineer.ServiceEngineerId) 
                        { 
                            serviceEngineerContainer.IsSelected = true
                            break
                        } 
                } 
            } 
            #endregion 
            #region event listeners 
     
            private void RotationPanelViewLoaded(object sender, RoutedEventArgs e) 
            { 
                cbxSearch.ItemFilter = (search, item) => 
                { 
                    var session = item as ServiceEngineer; 
                    if (session != null
                    { 
                        string filter = search.ToLower(); 
                        return 
                            (session.ServiceEngineerPerson.FirstName.ToLower().Contains(filter) || 
                             session.ServiceEngineerPerson.LastName.ToLower().Contains(filter) || 
                             session.ServiceEngineerPerson.Initials.ToLower().Contains(filter)); 
                    } 
                    return false
                }; 
                _rotationPanelViewLoaded = true
            } 
     
            private void RotationContainerLoaded(object sender, RoutedEventArgs e) 
            { 
                ((RadPanelBarItem)sender).Loaded -= RotationContainerLoaded; 
                SelectServiceEngineerInPanel((RadPanelBarItem)sender); 
            } 
     
            private void RotationContainerExpanded(object sender, RadRoutedEventArgs e) 
            { 
                ((RadPanelBarItem)sender).Expanded -= RotationContainerExpanded; 
                SelectServiceEngineerInPanel((RadPanelBarItem)sender); 
            } 
            #endregion 

    Where _searchForServiceEngineer comse from a searchcombo.

    The clue is these containers...

    /Øyvind
  5. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 18 Feb 2010 Link to this post

    Hello Øyvind,

    I'm glad you found a solution to your scenario. ContainerBindings are quite powerful functionality and getting used to them will save a lot of headbanging in the future. Let us know if you have additional questions or comments on the topic.

    Regards,
    Kiril Stanoev
    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.
Back to Top