0

I have a list box XAML of which looks like this

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <ListBox HorizontalAlignment="Left" Margin="12,12,0,0" Name="ComplaintslistBox" VerticalAlignment="Top">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button Width="400" BorderThickness="1">
                    <Button.Content>
                        <StackPanel Orientation="Horizontal">
                            <StackPanel>
                                <Image Source="{Binding Type}" Width="30" Height="30"></Image>
                            </StackPanel>
                            <StackPanel>
                                <TextBlock Text="{Binding Head}" Width="300"></TextBlock>
                            </StackPanel>
                        </StackPanel>
                    </Button.Content>
                </Button>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

now the problem is it is binding "Head" but not binding the "Type"

Type variable contains path to the image which is to be shown....

class looks like this

public class Complaints
{
    public string Id { get; set; }
    public string Type { get; set; }
    public string Head { get; set; }
    //public string Desc { get; set; }
    public string lat { get; set; }
    public string lon { get; set; }
    public string wardNo { get; set; }
    public string wardName { get; set; }
    public string creator { get; set; }
    public string url { get; set; }
    public string catId { get; set; }
    public string subCatId { get; set; }
    public int commentCount { get; set; }
    public int solutionCount { get; set; }
    public int affectedCount { get; set; }

    public Complaints() { }

    public Complaints(Complaints complaints)
    {
        this.Id = complaints.Id;
        this.Head = complaints.Head;
        //this.Desc = complaints.Desc;

        switch (complaints.Type)
        {
            case "Reported":
                this.Type = "icons/icon_red.png";
                break;
            case "Reopened":
                this.Type = "icons/icon_black.png";
                break;
            case "Resolved":
                this.Type = "icons/icon_green.png";
                break;
            default:
                this.Type = "icons/icon_blue.png";
                break;
        }

        this.wardNo = complaints.wardNo;
        this.wardName = complaints.wardName;

        this.lat = complaints.lat;
        this.lon = complaints.lon;

        this.catId = complaints.catId;
        this.subCatId = complaints.subCatId;

        this.url = complaints.url;
        this.creator = complaints.creator;

        this.affectedCount = complaints.affectedCount;
        this.commentCount = complaints.commentCount;
        this.solutionCount = complaints.solutionCount;
    }
}

Thanks

1Mayur
  • 3,419
  • 5
  • 40
  • 64

2 Answers2

1

Binding to an ImageSource might be easier than binding to a string.

You'll need something like this:

var ISC = new System.Windows.Media.ImageSourceConverter();

this.Type = (ImageSource)ISC.ConvertFromString(ImagePath);

where Type is:

public ImageSource Type { get; set; }

rather than string.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • The following question covers most of image binding scenarios: http://stackoverflow.com/questions/20586/wpf-image-urisource-and-data-binding – Amittai Shapira Dec 12 '11 at 12:19
  • Actually you should be able to bind to strings just fine, so what you are saying is kind of wrong. – H.B. Dec 12 '11 at 12:24
  • @Sirwani - Are these images resources in the project? If so see [H.B.'s answer](http://stackoverflow.com/a/8474372/59303). – ChrisF Dec 12 '11 at 13:24
  • Images are resources in build action but not acting like resources :( – 1Mayur Dec 13 '11 at 05:23
0

The question is where those images are located, if those are resources in the assembly you need to qualify the path using Pack URI syntax, in fact it can never hurt to fully qualify the path. So if you use resources it should be something like:

"pack://application:,,,/icons/icon_red.png"
H.B.
  • 166,899
  • 29
  • 327
  • 400