Question

Binded image refuses to be displayed. Could anyone give some hints?

This is XAML:

<ListBox Name="TransactionList" Margin="0,0,0,78">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Width="460" Height="100">
                        <Button.Content>
                            <StackPanel Orientation="Horizontal" Height="80" Width="400">
                                <Image Source="{Binding Pic}" Margin="2" Width="80" Height="80" Stretch="Fill" Style="{Binding}" />
                                <StackPanel Orientation="Vertical" Height="40">
                                    <StackPanel Orientation="Horizontal" Height="40">
                                        <TextBlock Width="100" FontSize="22" Text="{Binding Name}" Height="40" Style="{Binding}" />
                                        <TextBlock Width="200" FontSize="22" Text="{Binding Surname}" Height="40"/>                                            
                                    </StackPanel>
                                    <StackPanel Orientation="Horizontal" Height="40">
                                        <TextBlock Width="200" FontSize="22" Text="{Binding Number}" Height="40" Style="{Binding}" />
                                    </StackPanel>
                                </StackPanel>
                            </StackPanel>
                        </Button.Content>
                    </Button>    
                </DataTemplate>       
            </ListBox.ItemTemplate>
        </ListBox>

This is how it should look like

And this what I got

Image and Number are not displayed...

Here's the MainPage structure:

public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainPage_Loaded);

    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        List<Transaction> transactionList = new List<Transaction>();

        transactionList.Add(new Transaction("John", "Smith", "+38066565661", 1)); //'1' stands for Gender

        TransactionList.ItemsSource = transactionList;
    }

And a Transaction class:

public class Transaction
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Number { get; set; }
    public int Gender { get; set; }
    public string Pic { get; set; }

    public Transaction(String Name, String Surname, String Number, int Gender)
    {
        this.Name = Name;
        this.Surname = Surname;
        this.Number = Number;
        this.Gender = Gender;
        switch (Gender)
        {
            case 0:
                this.Pic = "Images/man.png";
                break;
            case 1:
                this.Pic = "Images/woman.png";
                break;
            default:
                this.Pic = "Images/unknown.png";
                break;
        }
    }
}
Était-ce utile?

La solution

The Pic property should be the type of URI not string

public Uri Pic { get; set; }

and

public Transaction(String Name, String Surname, String Number, int Gender)
{
    this.Name = Name;
    this.Surname = Surname;
    this.Number = Number;
    this.Gender = Gender;
    switch (Gender)
    {
        case 0:
            this.Pic = new Uri("Images/man.png", UriKind.Relative);
            break;
        case 1:
            this.Pic = new Uri("Images/woman.png", UriKind.Relative);
            break;
        default:
            this.Pic = new Uri("Images/unknown.png", UriKind.Relative);
            break;
    }
}

xaml

<Image Margin="2" Width="80" Height="80" Stretch="Fill" Style="{Binding}" >
  <Image.Source>
    <BitmapImage UriSource="{Binding Pic}" />
   </Image.Source>
</Image>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top