-1

XAML code:

<ListView x:Name="Toolbar" Grid.Row="1" ItemsSource="{Binding List}">
    <ListView.ItemTemplate>
        <DataTemplate>
        <StackPanel  MouseLeftButtonDown="Lamp_Click" Name="Lamp" Background="White" Width="34" Height="35">
            <Grid Height="30">
                <Image  Source="images/{Binding Path}">
                    
                </Image>
                <TextBlock Text="{Binding Name}" FontSize="7" TextAlignment="Center" Margin="-1,19,1,-12"></TextBlock>
            </Grid>
        </StackPanel>
       
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

C# code:

public class MyClass
{
    public static List<ViewModel> List{ get; set; } = GetAll();
    
    public static List<ViewModel> GetAll()
    {
       // get records from data base.

    }
}

This code has syntax error. How can I change images/{Binding path} to a valid. why this code has error? I'm really confused. Please help me.

Code Land
  • 41
  • 8

1 Answers1

0
<Image  Source="images/{Binding Path}">

XAML doesn't support this kind of half binding thing you're doing, you either bind something or you don't. And even if it did, it wouldn't work like that because Image.Source isn't a string, it's an ImageSource.

Instead you need to create a converter that converts your Path binding (a string) to an ImageSource by instantiating a BitmapImage and setting its source to load the image asynchronously.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • A regular string or Uri is enough. The binding itself will call the required converter. – EldHasp Sep 03 '21 at 18:05
  • `StringFormat` isn't enough to perform the concatenation, if that's what you mean. The implicit converter doesn't use it before performing the conversion. You need a converter. – Blindy Sep 03 '21 at 18:12
  • 1
    There is implicit conversion from `string`, `Uri` and `byte[]` to `ImageSource`, provided by ImageSourceConverter - a TypeConverter class. You do not need a Binding Converter. – Clemens Sep 03 '21 at 18:28
  • Feel free to try it without a converter, I did and it doesn't work. Or is the issue that you didn't read the question and that he wants to append a string to the binding? – Blindy Sep 03 '21 at 18:42
  • The Binding isn't valid - as you correctly point out. It should be `Source="{Binding Path}"` where the `images/` prefix becomes part of a relative or absolute file path, or a resource file pack URI, or whatever, returned by the Path property. That Binding will work without a Converter. The "*wouldn't work*" part of your answer is plain wrong. – Clemens Sep 03 '21 at 18:55
  • You may want to take a look at `[System.ComponentModel.TypeConverter(typeof(System.Windows.Media.ImageSourceConverter))]` on the [ImageSource](https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.imagesource?view=net-5.0) declaration - that provides the implicit conversion mentioned before. – Clemens Sep 03 '21 at 19:01
  • Thats clever. I think there was no way. – Code Land Sep 04 '21 at 12:38