簡體   English   中英

在WPF中創建控件實例

[英]Create control instance in WPF

我想在一個wrappanel中加載多個圖像,對於每個圖像,我用這個代碼顯示縮略圖和一些圖像細節

<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

我也試過樣式,但樣式不支持多個參數(指定文本塊:尺寸,大小,名稱,類型和大小)只有“TemplateBinding標簽”為1值

如何創建第一個代碼的實例以在運行時顯示多個圖像?

哇。 這看起來很難做事。 是時候接受WPF和XAML了。

我有一篇關於這個完全相同的事情的帖子還沒有完成。 我花時間為你完成它。 我甚至在你的例子中使用了你的XAML代碼片段(以及它的修改版本)。

http://www.wpfsharp.com/2012/10/23/displaying-images-from-a-folder-with-details-in-wpf/

Clemens在評論中使用ItemsControl是正確的。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM