绑定到 WPF XAML 中的动态对象

问题描述 投票:0回答:1

我正在实习初级 C# 开发人员的职位,并面临以下问题。在XAML布局中,项目中会生成相当多的动态块。它们不能通过另一个代码块的名称进行绑定。但我需要从外部动态块连接到它们。这还可以吗?

这是我需要绑定的代码部分。

<TreeView>
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type src:GroupsTreeNode}"
            ItemsSource="{Binding SubGroups}"
        >
            <StackPanel Orientation="Horizontal">
                <TextBlock>
                    <Run FontWeight="Bold" Text="{Binding NameCOM}"/>
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type src:SubGroupsTree}">
                <!---  There is a lot of other code in the project here --->
        <TextBlock Text={ Here I need the same value, like in Run }>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

我需要将 Text 块中的文本绑定到 Run 块中的文本。可以吗?

有什么方法可以将这段代码放入Binding中吗? - DataType =“{x:类型src:GroupsTreeNode}”

类似 - Text={Binding Source=src:GroupsTreeNode, Path=NameCOM} ?

我研究过很多网站上关于Binding的信息,但是到处都有相当简单的例子。有没有什么资源可以非常深入地揭示这个主题?

c# wpf xaml binding
1个回答
0
投票

您的解释不足以完全理解问题。如果我理解正确,那么尝试设置相同的绑定。如果两个模板都指定绑定到同一个 GroupsTreeNode 实例,则其 NameCOM 属性将充当绑定的值传递点:

    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type src:GroupsTreeNode}"
            ItemsSource="{Binding SubGroups}"
        >
            <StackPanel Orientation="Horizontal">
                <TextBlock>
                    <Run FontWeight="Bold" Text="{Binding NameCOM}"/>
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type src:SubGroupsTree}">
                <!---  There is a lot of other code in the project here --->
        <TextBlock Text="{Binding NameCOM}">
        </DataTemplate>
    </TreeView.Resources>
© www.soinside.com 2019 - 2024. All rights reserved.