绑定到 WPF Treeview 模板项

发布于 2024-11-02 00:05:36 字数 3179 浏览 1 评论 0原文

我有一个 TreeView,其数据上下文是使用

        LayoutRoot.DataContext = value

隐藏代码设置的。

CommandTreeViewModel 具有 IEnumerable(Of CommandViewModel)Commands 属性

CommandViewModel 又具有 CommandViewModel 的多个子级

在我的 XAML 中,我使用以下 XAML 将其转换为树项目

        <TreeView ItemsSource="{Binding}"
                  DataContext="{Binding FirstGeneration}"
                  x:Name="CommandTreeView">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <Border BorderThickness="1"
                            Width="200"
                            Margin="2"
                            CornerRadius="10,0,10,0">
                        <StackPanel Orientation="Horizontal"
                                <Image Source="{Binding Icon}"
                                       Width="24"
                                       Height="24" />
                            <TextBlock VerticalAlignment="Center"
                                       FontSize="13"
                                       Margin="10,0,0,0"
                                       Text="{Binding Name}"
                                       Foreground="White" />
                        </StackPanel>
                    </Border>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>

现在我在其他地方有一个图像和两个文本块,我想将它们绑定到所选树视图项目的原始数据源上的元素 - 具体来说, 图标描述名称。我尝试绑定它们,如下所示:

            <StackPanel Orientation="Vertical"
                DataContext="{Binding ElementName=CommandTreeView, Path=SelectedItem}">
                <Image x:Name="CommandIcon"
                       Width="64"
                       Height="64"
                       Source="{Binding XPath=@Icon}"></Image>
            </StackPanel>

TextBlocks 的 Text 属性相同。

当我单击树视图项目时,我在输出窗口中遇到以下异常...

System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Icon' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'Image' (Name='CommandIcon'); target property is 'Source' (type 'ImageSource') CommandViewModel:'BitBox.Core.CommandViewModel'
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Name' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'TextBlock' (Name='CommandTitle'); target property is 'Text' (type 'String') CommandViewModel:'BitBox.Core.CommandViewModel'
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Description' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'TextBlock' (Name='CommandBody'); target property is 'Text' (type 'String') CommandViewModel:'BitBox.Core.CommandViewModel'

I've got a TreeView whose data context is set using

        LayoutRoot.DataContext = value

from the code-behind.

CommandTreeViewModel has an Commands property of IEnumerable(Of CommandViewModel)

CommandViewModel in turn has multiple children of CommandViewModel

In My XAML, I convert this into tree items using the following XAML

        <TreeView ItemsSource="{Binding}"
                  DataContext="{Binding FirstGeneration}"
                  x:Name="CommandTreeView">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <Border BorderThickness="1"
                            Width="200"
                            Margin="2"
                            CornerRadius="10,0,10,0">
                        <StackPanel Orientation="Horizontal"
                                <Image Source="{Binding Icon}"
                                       Width="24"
                                       Height="24" />
                            <TextBlock VerticalAlignment="Center"
                                       FontSize="13"
                                       Margin="10,0,0,0"
                                       Text="{Binding Name}"
                                       Foreground="White" />
                        </StackPanel>
                    </Border>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>

Now I've got an image and two textblocks elsewhere which I want to bind to elements on the original datasource for the selected treeview item - specifically, Icon, Description, Name. I'm attempting to bind them as shown below:

            <StackPanel Orientation="Vertical"
                DataContext="{Binding ElementName=CommandTreeView, Path=SelectedItem}">
                <Image x:Name="CommandIcon"
                       Width="64"
                       Height="64"
                       Source="{Binding XPath=@Icon}"></Image>
            </StackPanel>

and the same with the TextBlocks 's Text property.

I'm getting the following exception in the output window when I click on a treeview item...

System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Icon' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'Image' (Name='CommandIcon'); target property is 'Source' (type 'ImageSource') CommandViewModel:'BitBox.Core.CommandViewModel'
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Name' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'TextBlock' (Name='CommandTitle'); target property is 'Text' (type 'String') CommandViewModel:'BitBox.Core.CommandViewModel'
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='@Description' BindingExpression:Path=/InnerText; DataItem='CommandViewModel' (HashCode=39320280); target element is 'TextBlock' (Name='CommandBody'); target property is 'Text' (type 'String') CommandViewModel:'BitBox.Core.CommandViewModel'

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

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

发布评论

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

评论(2

葬花如无物 2024-11-09 00:05:36

您的 XAML 在绑定中使用 XPath,该 XPath 仅在绑定到 XML 文档时使用:

{Binding XPath=@Icon}

请尝试执行此操作:

{Binding Path=Icon}

还可能发生其他情况。

Your XAML uses an XPath in the binding which is only used when binding to an XML document:

{Binding XPath=@Icon}

Try this instead:

{Binding Path=Icon}

There could be something else going on as well.

青丝拂面 2024-11-09 00:05:36

您是否基于某些 XML 构建 CommandViewModel 层次结构?因为如果是这种情况,您的 CommandViewModel 还应该具有一个类似于 XmlNode SourceNode 的属性,该属性链接到相应的 XmlNode,然后在您的 XAML 中您应该这样做:

       <StackPanel Orientation="Vertical"
            DataContext="{Binding ElementName=CommandTreeView, Path=SelectedItem.SourceNode}">
            <Image x:Name="CommandIcon"
                   Width="64"
                   Height="64"
                   Source="{Binding XPath=@Icon}"></Image>
        </StackPanel>

那么它应该可以工作。

或者,如果您根本不使用 XML,请按照 StellarEleven 的建议进行操作 - 从 {Binding XPath=@Icon} 中删除“X”和“@”。

Do you build your CommandViewModel hierarchy based on some XML? Because if that is the case your CommandViewModel should also have a property like XmlNode SourceNode that links to the corresponding XmlNode and then in your XAML you should do:

       <StackPanel Orientation="Vertical"
            DataContext="{Binding ElementName=CommandTreeView, Path=SelectedItem.SourceNode}">
            <Image x:Name="CommandIcon"
                   Width="64"
                   Height="64"
                   Source="{Binding XPath=@Icon}"></Image>
        </StackPanel>

then it should work.

Or if you don't use XML at all, do as StellarEleven suggested - remove the "X" and "@" from {Binding XPath=@Icon}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文