WPF表面列表框数据绑定-列表框内容未填充内容
我曾使用WPF表面列表框数据绑定-列表框内容未填充内容,wpf,xaml,listbox,Wpf,Xaml,Listbox,我曾使用XmlDataProvider从文件夹中获取图片,然后将它们显示在列表框中,但列表框出现了问题,但没有这些图片。图片位于项目目录中名为Resources的文件夹中 以下是代码,xmldata部分: <!-- List content --> <XmlDataProvider x:Key="tri" XPath="Root"> <x:XData> <Root x
XmlDataProvider
从文件夹中获取图片,然后将它们显示在列表框中,但列表框出现了问题,但没有这些图片。图片位于项目目录中名为Resources的文件夹中
以下是代码,xmldata部分:
<!-- List content -->
<XmlDataProvider x:Key="tri" XPath="Root">
<x:XData>
<Root xmlns="">
<Entry Name="Cone" Image="\Resources\cone.jpg" />
<Entry Name="Cube" Image="\Resources\cube.jpg" />
<Entry Name="Cylinder" Image="\Resources\cylinder.jpg" />
<Entry Name="Icosahedron" Image="\Resources\icosahedron.jpg" />
<Entry Name="Octahedron" Image="\Resources\octahedron.jpg" />
<Entry Name="Sphere" Image="\Resources\sphere.jpg" />
<Entry Name="Torus" Image="\Resources\torus.jpg" />
<Entry Name="YinYang" Image="\Resources\yinyang.jpg" />
</Root>
</x:XData>
</XmlDataProvider>
然后列表框部分:
<s:SurfaceListBox x:Name="triList" Grid.Row="1"
s:SurfaceDragDrop.DragCompleted="OntriListDragCompleted"
s:SurfaceDragDrop.DragCanceled="OntriListDragCanceled"
PreviewMouseLeftButtonDown="OntriListPreviewMouseLeftButtonDown"
PreviewMouseMove="OntriListPreviewMouseMove"
PreviewMouseLeftButtonUp="OntriListPreviewMouseLeftButtonUp"
ItemsSource="{Binding Source={StaticResource tri}, XPath=Entry}"
Style="{StaticResource triListStyle}"
PreviewTouchDown="OntriListPreviewTouchDown"
PreviewTouchMove="OntriListPreviewTouchMove"
PreviewTouchUp="OntriListPreviewTouchUp" Height="234" VerticalAlignment="Top" Visibility="Visible" ItemsPanel="{Binding}" AllowDrop="False" />
列表样式:
<Style x:Key="triListStyle" TargetType="{x:Type s:SurfaceListBox }">
<Setter Property="Background" Value="{DynamicResource {x:Static s:SurfaceColors.ListBoxItemBackgroundBrushKey}}" />
<Setter Property="SelectionMode" Value="Single" />
<Setter Property="Height" Value="234" />
<Setter Property="ItemTemplateSelector">
<Setter.Value>
<sc:triListTemplateSelector>
<sc:triListTemplateSelector.NormalItemTemplate>
<DataTemplate >
<StackPanel RenderTransformOrigin="0.5, 0.5"
Margin="7,0,0,0"
MinWidth="171" MaxWidth="171"
MinHeight="235" MaxHeight="235">
<Image Margin="14,21,21,11" Source="{Binding XPath=@Image}"
Height="149" Width="101" />
<TextBlock Text="{Binding XPath=@Name}"
MaxWidth="116"
FontSize="12"
Margin="21,0,21,21"
FontFamily="Segoe360"
TextAlignment="Center"
TextWrapping="Wrap"
Foreground="{DynamicResource {x:Static s:SurfaceColors.ListBoxItemForegroundBrushKey}}"
HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</sc:triListTemplateSelector.NormalItemTemplate>
<sc:triListTemplateSelector.StartingItemTemplate>
<DataTemplate>
<Grid Margin="17, 0, 0, -14">
<StackPanel RenderTransformOrigin="0.5, 0.5"
Margin="7,0,0,0"
MinWidth="171" MaxWidth="171"
MinHeight="235" MaxHeight="235">
<Image Margin="14,21,21,11"
Source="{Binding XPath=@Image}"
Height="149"
Width="101" />
<TextBlock Text="{Binding XPath=@Name}"
MaxWidth="116"
FontSize="12"
Margin="21,0,21,21"
FontFamily="Segoe360"
TextAlignment="Center"
TextWrapping="Wrap"
Foreground="{DynamicResource {x:Static s:SurfaceColors.ListBoxItemForegroundBrushKey}}"
HorizontalAlignment="Center" />
</StackPanel>
<Rectangle Fill="{DynamicResource {x:Static s:SurfaceColors.SurfaceWindowBackgroundBrushKey}}"
Width="17" HorizontalAlignment="Left" Margin="-26,-2.5,0,3" />
</Grid>
</DataTemplate>
</sc:triListTemplateSelector.StartingItemTemplate>
</sc:triListTemplateSelector>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<s:SurfaceScrollViewer Background="{TemplateBinding Background}"
VerticalScrollBarVisibility="Disabled"
HorizontalScrollBarVisibility="Hidden"
CanContentScroll="True">
<!--<sc:LoopingPanel IsItemsHost="True" /> -->
</s:SurfaceScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
你能帮我找出问题吗?我不知道为什么需要XmlDataProvider来在列表框中显示一些图像,但你需要的是一个带有控件的列表,该控件可以实际可视化每个图像:
<DataTemplate x:Key="imageTemplate">
<Image Source="{Binding XPath=@Image}"/>
</DataTemplate>
您可以通过设置属性在列表框中使用它:
<s:SurfaceListBox ... ItemTemplate="{StaticResource imageTemplate}"/>
另外,请确保将Visual Studio项目中的图像文件的资源设置为
Resource
。我想我不知道您提到的使用datatemplate的方式是什么,因为我有两个是trilist样式:您也可以发布该样式。只需编辑您的问题并添加(与之相关的部分)样式XAML。好吧,我不知道sc:triListTemplateSelector
到底做了什么,但显然它使用的数据模板做了正确的Source=“{Binding XPath=@Image}”
绑定。是否确保Visual Studio项目中的图像文件具有生成操作资源
?是的,很遗憾,我确定它们位于该文件夹中,并且生成操作设置为资源。存在templateselector,因此将执行startingitem模板(选定为起始项的项的模板)或normalitem模板(非起始项的项的模板)。我还将图像设置为起始项。