Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I have a WrapPanel control that's within a template that's within a custom control, I need to change it's orientation depending on a certain criteria being met.

HTML
<pan:Panorama x:Name="panMainContent" Grid.Column="0" Grid.Row="1" Margin="0,3"
              Foreground="Black" UseSnapBackScrolling="False" 
              VerticalAlignment="Top" IsManipulationEnabled="False"
              ScrollViewer.PanningMode="HorizontalOnly" 
              Friction="0.10000000000000002">
              <ListBox x:Name="lbCategories" BorderBrush="{x:Null}" 
                         ScrollViewer.VerticalScrollBarVisibility="Disabled" 
                         ScrollViewer.PanningMode="HorizontalOnly"
                         removed="{x:Null}" IsManipulationEnabled="True" 
                         ManipulationStarting="lbCategories_ManipulationStarting" 
                         ManipulationCompleted="lbCategories_ManipulationCompleted" 
                         ManipulationStarted="lbCategories_ManipulationStarted"
                 ItemContainerStyle="{StaticResourceListBoxtemStyleNoHighlighting}">
                         <ListBox.ItemsPanel>
                             <ItemsPanelTemplate>
                                 <WrapPanel Name="wp" IsItemsHost="True"
                                            Width="Auto" Orientation="Vertical"
                                            Height="{Binding ElementName=gridContent,Path=ActualHeight}"
                                        MaxHeight="500"
                                        ScrollViewer.PanningMode="HorizontalOnly"
                                        IsManipulationEnabled="True" ItemHeight="{Binding ElementName=tt, Path=ActualHeight}" >
                                </WrapPanel>
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <uc:TouchTile x:Name="tt" Click="Tile_Click"
                 PreviewMouseLeftButtonDown="TouchTile_PreviewMouseLeftButtonDown" 
                                           
                 PreviewMouseLeftButtonUp="TouchTile_PreviewMouseLeftButtonUp" 
                 ProcessLaunchCompletedEvent="tt_ProcessLaunchCompletedEvent" 
               ProcessLaunchStartedEvent="tt_ProcessLaunchStartedEvent" Width="180">
                                </uc:TouchTile>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
              </ListBox>
</pan:Panorama>


I am having trouble referencing the "wp" WrapPanel from the code behind, as it doesn't appear.

I'm assuming once I have the reference to this control I can literally just:

C#
if (criteria is met)
{
   //change orientation to horizontal
}
else
{
   //change orientation to vertical
}


Has anyone got any idea how I can refer to this control in the code? Many Thanks!
Posted
Updated 16-Oct-12 21:45pm
v3

I do not know why you want to do this from code behind. You can, but you can also do it in the viewmodel with binding. Here is your solution:

C#
if (criteria is met)
  wp.Orientation = Orientation.Horizontal;
else
  wp.Orientation = Orientation.Vertical;
 
Share this answer
 
Comments
MitchG92_24 17-Oct-12 3:14am    
I know how to do the actual change of orientation. But it won't allow me to reference the WrapPanel. I feel this is because it is in a template so isnt instantiated until runtime. I've tried finding the control by name way about it but no luck.
It is needed on the code behind because as the items within the WrapPanel change, it needs to switch orientation depending on that.
Clifford Nelson 17-Oct-12 12:31pm    
You did not have this information in the question. You still should have been able to do the same thing with Binding since the information would have been available when the control needed it.
I found how to do it whilst reading this article:

http://www.dev102.com/2008/08/07/how-to-access-a-wpf-control-which-is-located-in-a-datatemplate/[^]

All it takes is declaring a WrapPanel variable at the start of your code:

private WrapPanel wp = new WrapPanel();


And then hooking into the WrapPanel that already exists in your XAML, and accessing the Loaded event and casting the WrapPanel to tge variable just created:

C#
private void wp_Loaded(object sender, RoutedEventArgs e)
{
    wp = sender as WrapPanel;
}


Alot easier than I thought!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900