0

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>

你能帮我找出问题吗?

4

1 回答 1

1

不知道为什么需要 XmlDataProvider 的东西来在 ListBox 中显示一些图像,但你需要的是一个带有Image控件的DataTemplate ,它实际上可以可视化每个图像:

<DataTemplate x:Key="imageTemplate">
    <Image Source="{Binding XPath=@Image}"/>
</DataTemplate>

您可以通过设置ItemTemplate属性在 ListBox 中使用它:

<s:SurfaceListBox ... ItemTemplate="{StaticResource imageTemplate}"/>

Also make sure that the Build Action is set to Resource for the image files in your Visual Studio project.

于 2013-02-09T20:43:09.660 回答