在数据模板中访问WrapPanel并更改属性 [英] Accessing WrapPanel within datatemplate and changing properties

查看:49
本文介绍了在数据模板中访问WrapPanel并更改属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个WrapPanel控件,该控件位于自定义控件的模板中,我需要根据满足的特定条件来更改其方向.

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.

<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>



我无法从后面的代码中引用"wp" WrapPanel,因为它没有出现.

我假设一旦有了对此控件的引用,我就可以做到:



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:

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!

推荐答案

我不知道为什么要从后面的代码中做到这一点.可以,但是也可以在带有绑定的viewmodel中进行.这是您的解决方案:

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:

if (criteria is met)
  wp.Orientation = Orientation.Horizontal;
else
  wp.Orientation = Orientation.Vertical;


我在阅读本文时发现了如何做:

http://www.dev102.com/2008/08/07/how-to-access-a-wpf-control-which-is-located-in-a-datatemplate/ [
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();



然后挂接到XAML中已经存在的WrapPanel中,并访问Loaded事件并将WrapPanel强制转换为刚创建的tge变量:



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:

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



比我想象的要容易得多!



Alot easier than I thought!


这篇关于在数据模板中访问WrapPanel并更改属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆