Autor Beitrag
daeve
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 116
Erhaltene Danke: 3

Windows (XP Pro, 7 Ultimate x64)
C#,WPF,Java,ASP.Net, VS 2010 Ultimate (x86)
BeitragVerfasst: Fr 06.01.12 22:16 
Hallo zusammen

ich habe Bilder in einem SQL08 das ganze ist eine WCF app.

Nun zu meinem Problem, ich habe im WPF Client eine List<T> in der sind zwei Strings(Marke,Modell) und ein Byte[](Bild).

nun möchte ich ein Binding auf das Image Feld, wenn in der ListBox das Item gewechselt wird.

bei einem Label wäre das die lösung

ausblenden C#-Quelltext
1:
 <Label Content="{Binding ElementName=listBox1, Path=SelectedItem.Content, Mode=OneWay}"/>					


kann das auch über dem xaml gelöst werden ?

irgend wie so stellte ich mir das vor...
ausblenden C#-Quelltext
1:
 <Image Source="{Binding ..........					


könnt ihr mir helfen ?

Gruss David
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Sa 07.01.12 02:43 
Das geht genauso wie beim Label, nur dass du eben an den richtigen Propertytyp binden musst. Erstelle in der Klasse deiner Elemente eine read-only Property vom Typ BitmapSource, in deren Getter du das Bitmap aus dem Byte-Array erzeugst, und tada, fertig ist dein erstes ViewModel ;) .

_________________
>λ=

Für diesen Beitrag haben gedankt: daeve
daeve Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 116
Erhaltene Danke: 3

Windows (XP Pro, 7 Ultimate x64)
C#,WPF,Java,ASP.Net, VS 2010 Ultimate (x86)
BeitragVerfasst: Sa 07.01.12 11:54 
user profile iconKha hat folgendes geschrieben Zum zitierten Posting springen:
Das geht genauso wie beim Label, nur dass du eben an den richtigen Propertytyp binden musst. Erstelle in der Klasse deiner Elemente eine read-only Property vom Typ BitmapSource, in deren Getter du das Bitmap aus dem Byte-Array erzeugst, und tada, fertig ist dein erstes ViewModel ;) .


oke danke erstmal ;)

Habe das Feld angepasst und übergebe direkt ein Bitmap.
ausblenden C#-Quelltext
1:
2:
3:
public String Marke { get; set; }
public String Modell { get; set; }
public BitmapImage Image { get; set; }


nur das mit dem Binding klappt nicht so ganz...
Ich müsste ja irgend wie auf das Image Feld verweisen können, komme aber nicht drauf, habe schon etliche Varianten ausprobiert.
ausblenden C#-Quelltext
1:
<Image Source="{Binding Path=SelectedItem.Content, ElementName=listBox1}"					


Was ich auch noch sagen muss die Listbox habe ich so abgefüllt und kein binding gemacht.
Auch weil ich nicht wusste wie, den google hat nicht viel infos über List binding.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
 private void FillListBox()
        {
            listBox1.Items.Clear();
            foreach (var item in CarList)
            {
                ListBoxItem i = new ListBoxItem();
                i.Tag = item;
                i.Content = item.Modell;
                listBox1.Items.Add(i);
            }
        }


Sinnvoll wäre ja alles zu binden...
daeve Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 116
Erhaltene Danke: 3

Windows (XP Pro, 7 Ultimate x64)
C#,WPF,Java,ASP.Net, VS 2010 Ultimate (x86)
BeitragVerfasst: Sa 07.01.12 12:58 
Ich hab mir jetzt eine Liste erstellt die das INotifyPropertyChanged implementiert.
Diese wird auf einen Buttonclick mit Daten gefüllt.
ausblenden C#-Quelltext
1:
public List<CarImageViewModel> ImageList { get; set; }					


So wollte ich auf die Liste verweisen...
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
<ListBox ItemsSource="{Binding Path=ImageList}" Height="270" HorizontalAlignment="Left" Margin="25,91,0,0" Name="listBox1" VerticalAlignment="Top" Width="224" DataContext="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" Height="50">
                        <TextBlock Text="{Binding Path=Modell}"/>
                        <TextBlock Text="{Binding Path=Marke}" Margin="10,1,10,0"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>


Und so das Bild binden
ausblenden C#-Quelltext
1:
<Image Source="{Binding Path=SelectedItem.GetImage, ElementName=listBox1}" Height="270" HorizontalAlignment="Left" Margin="303,91,0,0" Name="imageCar" Stretch="Fill" VerticalAlignment="Top" Width="386" />					


aber keins von den Beiden Bindings funktioniert.
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Sa 07.01.12 13:02 
Dann schau mal in das VS-Output-Fenster, dort sollten alle Binding-Fehler aufgelistet werden. "GetImage" hört sich jedenfalls nicht nach einer Property an?
DataContext="{Binding}" tut übrigens nichts.

_________________
>λ=
daeve Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 116
Erhaltene Danke: 3

Windows (XP Pro, 7 Ultimate x64)
C#,WPF,Java,ASP.Net, VS 2010 Ultimate (x86)
BeitragVerfasst: Sa 07.01.12 13:21 
user profile iconKha hat folgendes geschrieben Zum zitierten Posting springen:
Dann schau mal in das VS-Output-Fenster, dort sollten alle Binding-Fehler aufgelistet werden. "GetImage" hört sich jedenfalls nicht nach einer Property an?
DataContext="{Binding}" tut übrigens nichts.


Leider kann ich im Output Fenster nichts finden, bis auf die ganzen Loaded Nachrichten und ein par thread has exited with code 0

und ja anstatt GetImage sollte nur Image dort stehen.

Ich bekomme keine Fehler, auch beim debuggen nicht und weiss halt nicht wo ich noch suchen kann...
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Sa 07.01.12 14:34 
In welcher Klasse ist diese ImageList-Property definiert, wo weist du den DataContext der ListBox zu?

_________________
>λ=
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4807
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Sa 07.01.12 15:39 
Hallo daeve,

detailiertere DataBinding-Fehler kannst du dir über die Option "WPF Trace Settings" ausgeben lassen, s. blogs.msdn.com/b/wpf...ght-application.aspx

Für diesen Beitrag haben gedankt: daeve
daeve Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 116
Erhaltene Danke: 3

Windows (XP Pro, 7 Ultimate x64)
C#,WPF,Java,ASP.Net, VS 2010 Ultimate (x86)
BeitragVerfasst: Sa 07.01.12 16:29 
user profile iconKha hat folgendes geschrieben Zum zitierten Posting springen:
In welcher Klasse ist diese ImageList-Property definiert, wo weist du den DataContext der ListBox zu?


diese ist in der MainWindows.xaml.cs definiert

der ListBox habe ich keinen DataContext zugewiesen, müsste ich das machen ? ich dachte itemsource würde reichen.

Wenn ich im code-behind:

ausblenden C#-Quelltext
1:
listBox1.itemsource = ImageList					


definiere dann funktioniert es, aber das ist ja nicht die richtige lösung...


Zuletzt bearbeitet von daeve am Sa 07.01.12 18:14, insgesamt 2-mal bearbeitet
daeve Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 116
Erhaltene Danke: 3

Windows (XP Pro, 7 Ultimate x64)
C#,WPF,Java,ASP.Net, VS 2010 Ultimate (x86)
BeitragVerfasst: Sa 07.01.12 17:17 
Das habe ich jetzt erhalten, was heisst das genau ?

System.Windows.Data Information: 41 : BindingExpression path error: 'ImageList' property not found for 'object' because data item is null. This could happen because the data provider has not produced any data yet. BindingExpression:Path=ImageList; DataItem=null; target element is 'ListBox' (Name='listBox1'); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=ImageList; DataItem=null; target element is 'ListBox' (Name='listBox1'); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=ImageList; DataItem=null; target element is 'ListBox' (Name='listBox1'); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ImageList; DataItem=null; target element is 'ListBox' (Name='listBox1'); target property is 'ItemsSource' (type 'IEnumerable')
daeve Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 116
Erhaltene Danke: 3

Windows (XP Pro, 7 Ultimate x64)
C#,WPF,Java,ASP.Net, VS 2010 Ultimate (x86)
BeitragVerfasst: Sa 07.01.12 19:54 
Habe es hinbekommen, musste eine Neue Klasse erstellen.
In der ich die Daten in die Liste abfülle und die Liste als Property deklariere.
Dann habe ich in der MainWindows Klasse den Daten Kontext angegeben( eine Instanz von der Klasse in der die Liste als Property definiert ist)

Danke für die Bemühungen