WPF树视图绑定

编程入门 行业动态 更新时间:2024-10-07 22:22:52
本文介绍了WPF树视图绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在网格中显示图像文件.它们将从树视图中选择为名称,并且树视图与xml文件绑定,我正在显示图像的名称,并且我想使用我在xml中定义的ref属性,即图像的路径. 请任何人理解我的问题,并为我提供解决方案. xml是:

< 图书馆 > ; < 类别 类别名称 =" 全部" 名称 =" 全部" > < 图书 类别名称 =" 故事" 名称 =" 和尚" ref = C:\ Users \ Public \ Pictures \ Sample Pictures \ page2/ > < 图书 类别名称 =" 故事" 名称 =" 照片" ref = C:\ Users \ Public \ Pictures \ Sample Pictures \ page3/ ">> ; < 图书 类别名称 =" 恐怖" 名称 =" Better You" ref = C:\ Users \ Public \ Pictures \ Sample Pictures \ page4/ > < 图书 类别名称 =" 恐怖" 名称 =" Dracula" ref = C:\ Users \ Public \ Pictures \ Sample Pictures \ page5/ ">> ; < /类别 > < /库 > 我将树形视图用作:

< Window.Resources > < HierarchicalDataTemplate ItemsSource =" {Binding Path = Elements}" x:Key =" 库" > < TreeViewItem 标题 =" {Binding Path = Attribute [Name] .Value}" / > < /HierarchicalDataTemplate > <!- < XmlDataProvider x:Key ="XMLdata"源="C:\ Users \ SBS \ Desktop \ wizaed.xml"></XmlDataProvider> -> < /Window.Resources >

< TreeView MouseUp ="_ treeView_MouseUp" Cursor ="Hand" x:Name ="_ treeView" Width ="160" Background =透明" Margin ="0, 10 , 10 ,10" BorderBrush ="Transparent" BorderThickness ="0 " ItemsSource ="{绑定 Path = Root .元素}" ItemTemplate ="{ StaticResource ResourceKey = Library} " />

后面的代码是: XDocument XDoc = XDocument.Parse(File.ReadAllText(@"C:\ Users \ Public \ Mylibrary.xml")); _treeView.DataContext = XDoc;

解决方案

您是否要实现这样的目标? < Window.Resources > < XmlDataProvider x:键 =" XMLdata" 源 =" C :\ DirectoryStructure \ Library.xml" / > < HierarchicalDataTemplate x:键 =" 库" ItemsSource =" {绑定XPath = Elements}" > < TreeViewItem > < TreeViewItem.Header > < StackPanel =" 水平" > < 图片 源 =" {Binding XPath = @ ref}" / > < TextBlock 文本 =" {Binding XPath = @ Name}" / > < /StackPanel > < /TreeViewItem.Header > < /TreeViewItem > < /HierarchicalDataTemplate > < /Window.Resources > < 网格 > < TreeView 光标 =" 手" x:Name =" _ treeView " 宽度 =" 160" 背景 =" 透明" 保证金 =" 0,10,10,10" BorderBrush =" 透明" BorderThickness =" 0" ItemsSource =" {Binding XPath =//图书,来源= {StaticResource XMLdata}}" ItemTemplate =" {StaticResource ResourceKey = Library} " / >

I want to show image files in grid. which are to be selected from the tree view as names and the tree view is binded with a xml file, i am showing the names of images and i want to use the ref attribute which i had defined in my xml which is path of the image. Please any one understand my problem and give me a solution to this. the xml is:

<Library> <Category CategoryName="All" Name="All"> <Book CategoryName="Story" Name="The Monk" ref=C:\Users\Public\Pictures\Sample Pictures\page2/> <Book CategoryName="Story" Name="Photos" ref=C:\Users\Public\Pictures\Sample Pictures\page3/> <Book CategoryName="Horror" Name="Better You" ref=C:\Users\Public\Pictures\Sample Pictures\page4/> <Book CategoryName="Horror" Name="Dracula" ref=C:\Users\Public\Pictures\Sample Pictures\page5/> </Category> </Library>

and i am using the tree view as:

<Window.Resources> <HierarchicalDataTemplate ItemsSource="{Binding Path=Elements}" x:Key="Library"> <TreeViewItem Header="{Binding Path=Attribute[Name].Value}"/> </HierarchicalDataTemplate> <!--<XmlDataProvider x:Key="XMLdata" Source="C:\Users\SBS\Desktop\wizaed.xml" ></XmlDataProvider>--> </Window.Resources>

<TreeView MouseUp="_treeView_MouseUp" Cursor="Hand" x:Name="_treeView" Width="160" Background="Transparent" Margin="0,10,10,10" BorderBrush="Transparent" BorderThickness="0" ItemsSource="{Binding Path=Root.Elements}" ItemTemplate="{StaticResource ResourceKey=Library}" />

and the code behind is: XDocument XDoc = XDocument.Parse(File.ReadAllText(@"C:\Users\Public\Mylibrary.xml")); _treeView.DataContext = XDoc;

解决方案

Are you trying to achieve something like this?

<Window.Resources> <XmlDataProvider x:Key="XMLdata" Source="C:\DirectoryStructure\Library.xml" /> <HierarchicalDataTemplate x:Key="Library" ItemsSource="{Binding XPath=Elements}"> <TreeViewItem> <TreeViewItem.Header> <StackPanel Orientation="Horizontal"> <Image Source="{Binding XPath=@ref}" /> <TextBlock Text="{Binding XPath=@Name}" /> </StackPanel> </TreeViewItem.Header> </TreeViewItem> </HierarchicalDataTemplate> </Window.Resources> <Grid> <TreeView Cursor="Hand" x:Name="_treeView" Width="160" Background="Transparent" Margin="0,10,10,10" BorderBrush="Transparent" BorderThickness="0" ItemsSource="{Binding XPath=//Book, Source={StaticResource XMLdata}}" ItemTemplate="{StaticResource ResourceKey=Library}" />

更多推荐

WPF树视图绑定

本文发布于:2023-11-12 20:47:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582494.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   绑定   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!