我想在网格中显示图像文件.它们将从树视图中选择为名称,并且树视图与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/ ">> ; < /类别 > < /库 > pre> 我将树形视图用作: < 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树视图绑定
发布评论