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

Expand panel and select item while using data binding

3 Answers 83 Views
Expander
This is a migrated thread and some comments may be shown as answers.
Øyvind Øyen
Top achievements
Rank 1
Øyvind Øyen asked on 16 Feb 2010, 05:41 PM
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


3 Answers, 1 is accepted

Sort by
0
Kiril Stanoev
Telerik team
answered on 17 Feb 2010, 02:15 PM
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.
0
Øyvind Øyen
Top achievements
Rank 1
answered on 18 Feb 2010, 02:47 AM
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
0
Kiril Stanoev
Telerik team
answered on 18 Feb 2010, 09:50 AM
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.
Tags
Expander
Asked by
Øyvind Øyen
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Øyvind Øyen
Top achievements
Rank 1
Share this question
or