在DataTemplate中获得控制权

时间:2016-05-06 17:47:08

标签: c# xaml win-universal-app

所以我通过以下代码在xaml中定义了一个FlipView:

<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),它不起作用。我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;
            }