WPF:为什么我的 DataTemplate 项找不到其父项?

发布于 2024-10-22 18:28:03 字数 1654 浏览 4 评论 0原文

我的 *.xaml 中有一个 DataTemplate,它是我的列表视图控件的数据模板:

<DataTemplate x:Key="listViewSourceItemTemplate" DataType="{x:Type this:SourceItemField}">
    <this:SourceItemField Background="LightPink">
    <i:Interaction.Behaviors>
        <ei:MouseDragElementBehavior/>
    </i:Interaction.Behaviors>
    <StackPanel Orientation="Horizontal" Margin="5,10,5,10" Background="LightGray">
        <TextBlock Text="{Binding Path=FieldType,StringFormat=\{0\}}" Margin="0,0,5,0" />
        <TextBlock Text="{Binding Path=ContextName,StringFormat=\{0\}}" Margin="0,0,5,0" />
        <TextBlock Text="{Binding Path=FieldName,StringFormat=\{0\}}" Margin="0,0,5,0" />
        <TextBlock Text="{Binding Path=ConnectionNumber,StringFormat=\{0\}}" Margin="0,0,5,0" />
    </StackPanel>
</this:SourceItemField>

在我的 C# 代码中,我有以下程序:

    public void SetPosition()
    {
        Center = this.TransformToAncestor(FindAncestor<Canvas>(this)).Transform(new Point(this.ActualWidth / 2, this.ActualHeight / 2));
    }


    // walk up the visual tree for parent of type T
    private static T FindAncestor<T>(DependencyObject current)
        where T : DependencyObject
    {
        do
        {
            if (current is T)
            {
                return (T)current;
            }
            current = VisualTreeHelper.GetParent(current);
        }
        while (current != null);
        return null;
    }

“this”是 SourceItemField,为什么 VisualTreeHelper.GetParent() 将其祖先返回为“null”?为什么这个 SourceItemField 的父级不是我的列表视图?

I have a DataTemplate in my *.xaml, it is the datatemplate for my listview control:

<DataTemplate x:Key="listViewSourceItemTemplate" DataType="{x:Type this:SourceItemField}">
    <this:SourceItemField Background="LightPink">
    <i:Interaction.Behaviors>
        <ei:MouseDragElementBehavior/>
    </i:Interaction.Behaviors>
    <StackPanel Orientation="Horizontal" Margin="5,10,5,10" Background="LightGray">
        <TextBlock Text="{Binding Path=FieldType,StringFormat=\{0\}}" Margin="0,0,5,0" />
        <TextBlock Text="{Binding Path=ContextName,StringFormat=\{0\}}" Margin="0,0,5,0" />
        <TextBlock Text="{Binding Path=FieldName,StringFormat=\{0\}}" Margin="0,0,5,0" />
        <TextBlock Text="{Binding Path=ConnectionNumber,StringFormat=\{0\}}" Margin="0,0,5,0" />
    </StackPanel>
</this:SourceItemField>

In my C# code, I have following program:

    public void SetPosition()
    {
        Center = this.TransformToAncestor(FindAncestor<Canvas>(this)).Transform(new Point(this.ActualWidth / 2, this.ActualHeight / 2));
    }


    // walk up the visual tree for parent of type T
    private static T FindAncestor<T>(DependencyObject current)
        where T : DependencyObject
    {
        do
        {
            if (current is T)
            {
                return (T)current;
            }
            current = VisualTreeHelper.GetParent(current);
        }
        while (current != null);
        return null;
    }

"this" is SourceItemField, why VisualTreeHelper.GetParent() return its ancestor as "null"? Why this SourceItemField's parent is not my listview?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-10-29 18:28:03

您确定要查找 Canvas 而不是 ListView 作为祖先吗?

即而不是这个:

公共无效SetPosition()
{
Center = this.TransformToAncestor(FindAncestor<Canvas>(this)).Transform(new
点(this.实际宽度/2,
this.实际高度 / 2));
}

这个:

public void SetPosition() 
{ 
    Center = this.TransformToAncestor(FindAncestor<ListView>(this)).Transform(new Point(this.ActualWidth / 2, this.ActualHeight / 2)); 
} 

Are you sure you want to find Canvas and not the ListView as ancestor?




i.e. instead of this:

public void SetPosition()
{

Center = this.TransformToAncestor(FindAncestor<Canvas>(this)).Transform(new
Point(this.ActualWidth / 2,
this.ActualHeight / 2));
}


this:

public void SetPosition() 
{ 
    Center = this.TransformToAncestor(FindAncestor<ListView>(this)).Transform(new Point(this.ActualWidth / 2, this.ActualHeight / 2)); 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文