2016-05-06 5 views
0

Так у меня есть FlipView определены в XAML следующим кодом:Получить контроль внутри DataTemplate

<FlipView x:Name="carrousel" Height="175" Background="Transparent" Margin="0,20,0,0"> 
    <FlipView.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Rectangle x:Name="profile" Stroke="White" StrokeThickness="0" HorizontalAlignment="Center" VerticalAlignment="Center" Width="175" Height="175" Canvas.ZIndex="1" RadiusX="88" RadiusY="88" Tapped="profile_Tapped"/> 
      </Grid> 
     </DataTemplate> 
    </FlipView.ItemTemplate> 
</FlipView> 

Когда пользователь нажимает на прямоугольнике, это анимированные, чтобы стать больше, но я также хочу, чтобы все остальные прямоугольники каждый другой FlipViewItem тоже изменит размер. Как я могу это достичь? Я пробовал:

foreach(FlipViewItem fvi in carrousel.Items) 
{ 
    Rectangle g = (fvi.Content as Grid).FindName("profile") as Rectangle; 
    g.Width = double; 
    g.Height = double; 
} 

Но, видя, как мой FlipView не содержит FlipViewItems но пользовательские классы я привязываться к нему (которые, очевидно, не имеют .content), она не работает. Как я могу заставить это работать?

+0

Просто я любопытный. Если посмотреть в свое контрольное имя, мне кажется, что вы пытаетесь добиться эффекта/контроля карусели. Это верно? –

+0

Я пытался изменить свой flipview, чтобы получить аналогичный эффект, но это вроде как неудачно, поэтому я оставил и сохранил имя. – user2950509

+0

для карусельного эффекта вы можете следовать [это] https://comentsys.wordpress.com/2015/05/26/windows-10-universal-windows-platform-carousel-control/ или [this] http: // stackoverflow .com/questions/35008586/3d-carousel-control-available-for-uwp. –

ответ

0
foreach(var fvi in carrousel.Items) 
{ 
FlipViewItem item=carrousel.ContainerFromItem(fvi); 
var rectangle =FindElementInVisualTree<Rectangle>(item); 



//Or without VisualTreeHelper you can do like what were you trying before 
Rectangle g = (item.Content as Grid).FindName("profile") as Rectangle; 
g.Width = double; 
g.Height = double; 
} 

    private T FindElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject 
      { 
       var count = VisualTreeHelper.GetChildrenCount(parentElement); 
       if (count == 0) return null; 

       for (int i = 0; i < count; i++) 
       { 
        var child = VisualTreeHelper.GetChild(parentElement, i); 
        if (child != null && child is T) 
         return (T)child; 
        else 
        { 
         var result = FindElementInVisualTree<T>(child); 
         if (result != null) 
          return result; 
        } 
       } 
       return null; 
      } 
0

Это решение является поворотным решением (потому что я не мог управлять относительной привязкой).

<Grid Background="{ThemeResource SystemControlBackgroundListMediumBrush}"> 
    <StackPanel Margin="100,10,10,10"> 
     <FlipView x:Name="carrousel" Height="350" Width="350" Background="Red" Margin="0,20,0,0"> 
      <FlipView.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <Rectangle x:Name="profile" Stroke="White" StrokeThickness="1" Fill="Aqua" 
            HorizontalAlignment="Center" VerticalAlignment="Center" 
            Width="{Binding ElementName=RefValueRect, Path=Width, Mode=OneWay}" 
            Height="{Binding ElementName=RefValueRect, Path=Height, Mode=OneWay}" Canvas.ZIndex="1" RadiusX="88" 
            RadiusY="88" Tapped="profile_Tapped"/> 
        </Grid> 
       </DataTemplate> 
      </FlipView.ItemTemplate> 
     </FlipView> 
    </StackPanel> 
    <Rectangle x:Name="RefValueRect" Width="175" Height="175" Visibility="Collapsed" /> 
</Grid> 

код за

private void profile_Tapped(object sender, TappedRoutedEventArgs e) 
{ 
    RefValueRect.Width *= 2; 
    RefValueRect.Height *= 2; 
} 
Смежные вопросы