开发者

Setting a WPF Image in XAML to a property

I have ListBox in my WPF project that gets set to a datasource of "MyObjectCollection". I have managed to get the ListBox to display my collection, and each item to display two string properties from the object. The object also contains an Image, ho开发者_JS百科w do i get the image to display in the ListBox?

I am currently using the below code to bind to my DataSource

<UserControl.Resources>
        <DataTemplate x:Key="CustomerTemplate">
            <Border BorderThickness="2" BorderBrush="silver" CornerRadius="5" Padding="1"
                        HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Grid>
                    <Image Source="{Binding Artwork}" Tag="{Binding Artwork}" VerticalAlignment="Stretch" ></Image>
                        <TextBlock Text="{Binding Name}"  Foreground="#515151"
                       FontSize="16" HorizontalAlignment="Stretch"
                       FontWeight="Bold" />
                    <TextBlock Text="{Binding Length}" Foreground="#515151" Margin="0,25,0,0"
                       FontSize="10" HorizontalAlignment="Stretch"
                       FontWeight="Bold" />
                </Grid>
            </Border>
        </DataTemplate>
</UserControl.Resources>

Thanks, Ben


It depends on the image type in your collection.

If it is a path string to a file or if it is a byte array.

You should use ValueConverter for your image binding.

Take a look at ValueConverter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜