2014-04-08 4 views
5

पर छवि जोड़ना मैं वर्तमान में एक सी # डब्ल्यूपीएफ अनुप्रयोग पर काम कर रहा हूं जहां मैं एक छवि जोड़ने की कोशिश कर रहा हूं, उसके बाद प्रत्येक सूची आइटम में कुछ पाठ के बाद।ListBox आइटम

मुझे पाठ के लिए बाध्यकारी काम मिल गया है लेकिन छवि प्रदर्शित नहीं होती है।

नीचे मेरी XAML है:

<Window x:Class="ServerAdministrator.FtpDirectoryListing" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:ServerAdministrator" 
     Title="FTP Directory Listing" Height="391" Width="599"> 
    <Grid> 
     <StatusBar Height="30" Margin="0,322,0,0" Name="statusBar1" VerticalAlignment="Top" /> 
     <ToolBar Height="26" Name="toolBar1" VerticalAlignment="Top" /> 
     <TextBox Height="23" HorizontalAlignment="Left" Margin="12,32,0,0" Name="textBox1" VerticalAlignment="Top" Width="517" /> 
     <ListBox x:Name="lstDirecotryListing" Height="233" HorizontalAlignment="Left" Margin="12,61,0,0" VerticalAlignment="Top" Width="553"> 
      <ListBox.ItemTemplate> 
       <DataTemplate DataType="{x:Type local:DirectoryListing}"> 
        <StackPanel> 
         <TextBlock Margin="3" Text="{Binding path}" /> 
         <ContentControl Margin="3" Content="{Binding image}" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
</Window> 

नीचे है मेरी DirectoryListing वर्ग

class DirectoryListing 
    { 
     public string path {get; set;} 
     public Image image{get; set;} 
     public DirectoryListing(Image imgage, String path) 
     { 
      this.image = image; 
      this.path = path; 
     } 
    } 

नीचे कैसे मैं लिस्टबॉक्स

Image image = new Image(); 
      BitmapImage bi = new BitmapImage(new Uri(@"C:\Users\Chris\Documents\Visual Studio 2010\Projects\ServerAdministrator\ServerAdministrator\bin\Debug\images\directory.png")); 
      image.Source = bi; 
      lstDirecotryListing.Items.Add(new DirectoryListing(image, "hello")); 

पाठ जोड़ा जा रहा है में आइटम डाले रहा है ठीक है लेकिन छवि नहीं।

मुझे यकीन है कि अगर यह संबंधित है नहीं कर रहा हूँ, लेकिन मैं VS2010 में कंसोल आउटपुट में निम्नलिखित मिल

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ComboBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ComboBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')

किसी भी मदद के लिए धन्यवाद प्रदान कर सकते हैं

अद्यतन

मैं क्लेमेंस के जवाब के लिए यह काम कर रहा है, अभी भी वही दो चर का उपयोग कर रहा है क्योंकि पथ छवि का मार्ग नहीं है, लेकिन वैसे भी, यह अब छवि और पाठ प्रदर्शित कर रहा है।

समस्या यह है कि यह पाठ प्रदर्शित करता है और तस्वीर नीचे है, मुझे तस्वीर और पाठ पक्ष को तरफ दिखाने की ज़रूरत है, मैं यह कैसे कर सकता हूं?

+0

अपवाद इस कॉम्बो बॉक्स आइटम पर होरिज़ोंटलकंटेंट एलाइनमेंट के लिए है, इस समस्या से संबंधित नहीं है। आपका कोड ठीक दिखता है, मैं कुछ और देखूंगा और मुझे आशा है कि आपको जवाब मिलेगा! – BradleyDotNET

+0

क्या आप 'एक्सएएमएल' पोस्ट कर सकते हैं जहां 'कॉम्बोबॉक्स इटैम' को –

+0

परिभाषित किया गया है, मैंने फिर से देखा है। कॉम्बो बॉक्स के बारे में वह संदेश, पिछले संवाद के लिए है, समस्या – Boardy

उत्तर

7

इस के लिए अपने दृश्य मॉडल कम करें:

public class DirectoryListing 
{ 
    public string Name { get; set; } 
    public string Path { get; set; } 
} 

और इस के लिए अपने DataTemplate बदलने के लिए:

<ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Margin="3" Text="{Binding Name}"/> 
      <Image Margin="3" Source="{Binding Path}"/> 
     </StackPanel> 
    </DataTemplate> 
</ListBox.ItemTemplate> 

निर्मित जैसे रूपांतरण स्वचालित रूप से फ़ाइल पथ स्ट्रिंग से एक ImageSource पैदा करेगा।

+0

धन्यवाद यह काम करता है, एक दूसरी समस्या है, मैंने अपना प्रश्न – Boardy

+0

अपडेट किया है 'ओरिएंटेशन = "क्षैतिज" StackPanel। – Clemens

+0

इस तरह के रूप में सरल धन्यवाद। आपकी मदद के लिए धन्यवाद बहुत सराहना की – Boardy

संबंधित मुद्दे