在WPF

2012-10-23 217 views
0

創建控件實例我想加載wrappanel內的多個圖像,每個圖像I顯示縮略圖,使用此代碼在WPF

<Border BorderThickness="1" BorderBrush="#FFD0D1D7" Padding="5" Margin="10,10,0,0"> 
    <StackPanel Orientation="Horizontal"> 
     <!--image and dimensions--> 
     <Grid Width="88" Height="55"> 
      <Image Source="C:\img1.jpg" Width="88" Height="55"/> 
      <TextBlock Background="#B2000000" Foreground="White" Height="16" TextAlignment="Center" VerticalAlignment="Bottom">1280x1024</TextBlock> 
     </Grid> 
     <!--name, type and size--> 
     <StackPanel Orientation="Vertical" Margin="5,0,0,0" VerticalAlignment="Center"> 
      <TextBlock Margin="1" Foreground="#FF787878">img13.jpg</TextBlock> 
      <TextBlock Margin="1" Foreground="#FF787878">Type: JPEG</TextBlock> 
      <TextBlock Margin="1" Foreground="#FF787878">Size: 321 KB</TextBlock> 
     </StackPanel> 
    </StackPanel> 
</Border> 

但圖像是在運行時加載一些圖像細節,我需要一些方法來創建上述代碼的情況下,顯示圖像,尺寸,名稱,類型和大小

我試過這個解決方案https://stackoverflow.com/a/4991028/962284

StringBuilder sb = new StringBuilder(); 
// use xaml to declare a button as string containing xaml 
sb.Append(@"<Border BorderThickness='1' BorderBrush='#FFD0D1D7' Padding='5' Margin='10,10,0,0'> 
      <StackPanel Orientation='Horizontal'> 
       <!--image and dimensions--> 
       <Grid Width='88' Height='55'> 
        <Image Source='C:\img1.jpg' Width='88' Height='55'/> 
        <TextBlock Background='#B2000000' Foreground='White' Height='16' TextAlignment='Center' VerticalAlignment='Bottom'>1280x1024</TextBlock> 
       </Grid> 
       <!--name, type and size--> 
       <StackPanel Orientation='Vertical' Margin='5,0,0,0' VerticalAlignment='Center'> 
        <TextBlock Margin='1' Foreground='#FF787878'>img13.jpg</TextBlock> 
        <TextBlock Margin='1' Foreground='#FF787878'>Type: JPEG</TextBlock> 
        <TextBlock Margin='1' Foreground='#FF787878'>Size: 321 KB</TextBlock> 
       </StackPanel> 
      </StackPanel> 
     </Border>"); 

FrameworkElement thumb = (FrameworkElement)System.Windows.Markup.XamlReader.Parse(sb.ToString()); 
ThumbnailContainer.Children.Add(thumb); 

,但我得到以下錯誤 System.Windows.Markup.XamlParseException

我也試圖與風格,但犯規支持多種參數樣式(指定的TextBlocks:尺寸,大小,名稱,類型和大小)只是「TemplateBinding標記」 1個值

什麼我可以創建第一個代碼的實例以在運行時顯示多個圖像嗎?

+0

您需要的命名空間添加到您的XAML字符串 – Ondra

+1

但更好的辦法是使用和控制插入到WrapPanel插入某種模式(C#類),而不是DataTemplate中。這就是WPF的設計目的。 – Ondra

+3

你的方法是完全錯誤的。您應該使用[ItemsControl](http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx),將其ItemsPanel屬性設置爲WrapPanel,併爲其定義適當的DataTemplate你的數據。獲取更多信息[這裏是關於數據模板](http://msdn.microsoft.com/en-us/library/ms742521.aspx) – Clemens

回答

1

是的,你的方法是錯誤的,你應該以其他方式做到這一點,但要讓你的代碼片段正常工作,請嘗試將xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"添加到你正在構建的字符串中的Border元素。我懷疑這是錯誤。