Bonjour
pour afficher des images dans un listbox j'ai créé un objectdataprovider qui utilise la méthode Directory.getfiles(). cette méthode remonte un tableau string qui s'affiche bien dans le listbox
Pour afficher cette liste sous forme d'image j'utilise un datatemplate qui via un converter devait afficher cette liste sous forme d'images
mon problème est que je n'arrive pas à découvrir la cause de l'erreur qui apparait lorsque je lie mon itemstemplate à mon datatemplate
l'erreur est la suivante

System.Reflection.TargetInvocationException
Une exception a été levée par la cible d'un appel.
à System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
à System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
à System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
à System.Delegate.DynamicInvokeImpl(Object[] args)
à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
à MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)


System.Windows.Markup.XamlParseException
La propriété Set 'System.Windows.Data.Binding.Converter' a levé une exception.
à System.Windows.Markup.XamlReader.RewrapException(Exception e, Uri baseUri)
à System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
à System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
à System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
à System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
à System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
à System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
à System.Windows.FrameworkElement.ApplyTemplate()
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Border.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Control.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
à System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
à System.Windows.Controls.ScrollContentPresenter.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
à System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
à System.Windows.Controls.Grid.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.ScrollViewer.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à Microsoft.Windows.Themes.ClassicBorderDecorator.MeasureOverride(Size availableSize)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Control.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
à System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
à System.Windows.Controls.Grid.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
à System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Grid.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Border.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Control.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
à System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
à System.Windows.Controls.Grid.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Border.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Border.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.DockPanel.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Border.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Grid.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Control.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Border.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à MS.Internal.Designer.ZoomableViewPresenter.DesignerBackground.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Decorator.MeasureOverride(Size constraint)
à Microsoft.Windows.Design.Interaction.DesignerView.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à MS.Internal.Designer.Viewport.MeasureOverride(Size availableSize)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
à System.Windows.Controls.ScrollContentPresenter.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
à System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
à System.Windows.Controls.Grid.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.ScrollViewer.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Grid.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
à System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Controls.Control.MeasureOverride(Size constraint)
à System.Windows.FrameworkElement.MeasureCore(Size availableSize)
à System.Windows.UIElement.Measure(Size availableSize)
à System.Windows.Interop.HwndSource.SetLayoutSize()
à System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
à System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
à MS.Internal.DeferredHwndSource.ProcessQueue(Object sender, EventArgs e)


System.InvalidCastException
Impossible d'effectuer un cast d'un objet de type 'System.String' en type 'System.Windows.Data.IValueConverter'.
à System.Windows.Baml2006.WpfSharedBamlSchemaContext.<Create_BamlProperty_Binding_Converter>b__14c(Object target, Object value)
à System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value)
à MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
à MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
mon code est le suivant
XAML
Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System.IO;assembly=mscorlib"
        xmlns:sy="clr-namespace:System;assembly=mscorlib"
        xmlns:c="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <c:convert x:Key="Convert"/>
        <ObjectDataProvider x:Key="Sources" MethodName="GetFiles" ObjectType="{x:Type sys:Directory}">
            <ObjectDataProvider.MethodParameters>
                <sy:String>\\Patricia-pc\h\Photos\Indes</sy:String>
                <sy:String>*.jpg</sy:String>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
 
        <DataTemplate x:Key="Modele">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding   Converter=Convert}"/>
 
            </StackPanel>
        </DataTemplate>
 
    </Window.Resources>
    <Grid>
        <ListBox Height="232" HorizontalAlignment="Left" SelectionChanged="listBox1_SelectionChanged"
                 Margin="123,15,0,0" Name="listBox1" VerticalAlignment="Top" ItemsSource="{Binding Source={StaticResource Sources}}"
                 Width="255" ItemTemplate="{Binding Source={StaticResource Modele}}"
 
       />
        <Image Height="56" HorizontalAlignment="Left" Margin="381,94,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="96"/>
 
 
    </Grid>
</Window>

CODE .CS
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace WpfApplication2
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
 
 
        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
 
        }
 
    }
 
 
    public class convert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
               {
               if (value == null)
                {
                     return null;
                 }
 
                if (value is string)
                {
                    value = new Uri((string)value);
                }
 
              if (value is Uri)
            {                
                   BitmapImage bi = new BitmapImage();
                  bi.BeginInit();
                    bi.DecodePixelWidth = 80;
                    //bi.DecodePixelHeight = 60;                
                    bi.UriSource = (Uri)value;
                    bi.EndInit();
                    return bi;
                }
 
                return null;
           }
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}

Par avance merci de votre aide