創建控件實例我想加載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);
,但我得到以下錯誤
我也試圖與風格,但犯規支持多種參數樣式(指定的TextBlocks:尺寸,大小,名稱,類型和大小)只是「TemplateBinding標記」 1個值
什麼我可以創建第一個代碼的實例以在運行時顯示多個圖像嗎?
您需要的命名空間添加到您的XAML字符串 – Ondra
但更好的辦法是使用和控制插入到WrapPanel插入某種模式(C#類),而不是DataTemplate中。這就是WPF的設計目的。 – Ondra
你的方法是完全錯誤的。您應該使用[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