user962284
user962284

Reputation: 678

Create control instance in WPF

I want to load multiple images inside a wrappanel, for each image I show a thumbnail and some image details with this code

<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>

But the images are loaded at runtime, and I need some way to create instances of the above code to show the image, dimensions, name, type and size

I tried this solution 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);

but I get the following error System.Windows.Markup.XamlParseException

I also tried with styles, but styles doesnt support multiple parameters (to specify the textblocks: dimensions, size, name, type and size) just "TemplateBinding Tag" for 1 value

What can I do to create instances of the first code to show multiple images at runtime?

Upvotes: 0

Views: 2479

Answers (2)

Tejas Sharma
Tejas Sharma

Reputation: 3440

Yes your approach is wrong and you should be doing this some other way but to get your code snippet to work try adding xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" to your Border element in the string you are building. I suspect that is the error.

Upvotes: 1

Rhyous
Rhyous

Reputation: 6690

Wow. That so looks like the hard way to do things. Time to embrace WPF and XAML.

I had a post about this exact same thing that wasn't quite finished. I took time to finish it for you. I even used your XAML snippet (well, a modified version of it) in the example, just for you.

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

Clemens is correct in his comment to use an ItemsControl.

Upvotes: 1

Related Questions