首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么Projection属性为null?

为什么Projection属性为null?
EN

Stack Overflow用户
提问于 2011-11-14 02:26:22
回答 1查看 178关注 0票数 0
代码语言:javascript
运行
复制
<ListBox Name="listBoxButtons"
         Height="700">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border Background="{StaticResource PhoneAccentBrush}"
                    Name="border"
                    Width="432" Height="62"
                    Margin="6" Padding="12,0,0,6">
                <TextBlock Text="{Binding}" 
                           Foreground="#FFFFFF" FontSize="26.667"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Bottom"
                           FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
                <Border.Projection>
                    <PlaneProjection RotationX="-60"/>
                </Border.Projection>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

代码:

代码语言:javascript
运行
复制
private void ShowAnim()
{
    IEasingFunction quadraticEase = new QuadraticEase { EasingMode = EasingMode.EaseOut };
    Storyboard _swivelShow = new Storyboard();
    foreach (var item in this.listBoxButtons.Items)
    {
        UIElement container = listBoxButtons.ItemContainerGenerator.ContainerFromItem(item) as UIElement;
        if (container != null)
        {
            Border content = VisualTreeHelper.GetChild(container, 0) as Border;
            if (content != null)
            {
                DoubleAnimationUsingKeyFrames showAnimation = new DoubleAnimationUsingKeyFrames();

                EasingDoubleKeyFrame showKeyFrame1 = new EasingDoubleKeyFrame();
                showKeyFrame1.KeyTime = TimeSpan.FromMilliseconds(0);
                showKeyFrame1.Value = -60;
                showKeyFrame1.EasingFunction = quadraticEase;

                EasingDoubleKeyFrame showKeyFrame2 = new EasingDoubleKeyFrame();
                showKeyFrame2.KeyTime = TimeSpan.FromMilliseconds(85);
                showKeyFrame2.Value = 0;
                showKeyFrame2.EasingFunction = quadraticEase;

                showAnimation.KeyFrames.Add(showKeyFrame1);
                showAnimation.KeyFrames.Add(showKeyFrame2);

                Storyboard.SetTargetProperty(showAnimation, new PropertyPath(PlaneProjection.RotationXProperty));
                Storyboard.SetTarget(showAnimation, content.Projection);

                _swivelShow.Children.Add(showAnimation);
            }
        }
    }
    _swivelShow.Begin();
}

但是:Storyboard.SetTarget(showAnimation, content.Projection)抛出了一个异常。content.Projectionnull。这怎么会发生呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-14 04:52:04

您看到的是错误的Border元素。在您的案例中,项目容器的层次结构类似于Border -> ContentControl -> ContentPresenter -> container (这是您的)。因此,您必须深入容器的子层次结构以找到您想要的边框。

下面的代码递归地搜索UIElement的子级,并给出一些调试输出,以便您可以看到它有多深入。当它找到名为“border”的控件时,它将停止:

代码语言:javascript
运行
复制
    private UIElement GetMyBorder(UIElement container)
    {
        if (container is FrameworkElement && ((FrameworkElement)container).Name == "border")
            return container;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(container); i++)
        {
            var child = (FrameworkElement)VisualTreeHelper.GetChild(container, i);
            System.Diagnostics.Debug.WriteLine("Found child "+ child.ToString());

            System.Diagnostics.Debug.WriteLine("Going one level deeper...");
            UIElement foundElement = GetMyBorder(child);
            if (foundElement != null)
                return foundElement;
        }
        return null;
    }

要使用它:

代码语言:javascript
运行
复制
    Border content = (Border)GetMyBorder(container);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8113670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档