3

I have an application with a local database (SQL CE), and I want to bind a listbox to a table (Car). However, I can't save WriteableBitmap to my local database so I decided to transform the image to an array of bytes so I need to call the method dynamically. Here's what I've got so far.

[Table]
class Car
{
    [Column (IsPrimaryKey=true, IsDbGenerated=true, CanBeNull=false, AutoSync = AutoSync.OnInsert)]
    public int ID { get; set; }

    [Column (CanBeNull=false)]
    public int MakeID { get; set; }

    [Column(CanBeNull = false)]
    public int ModelID { get; set; }

    [Column(CanBeNull = false)]
    public int AccountID { get; set; }

    [Column(CanBeNull = false)]
    public int Year { get; set; }

    [Column]
    public string Name { get; set; }

    [Column]
    public byte[] PicBytes { get; set; }

    private EntitySet<Maintenance> maintenance;
    [Association(Storage = "maintenance", ThisKey = "ID", OtherKey = "CarID")]
    public EntitySet<Maintenance> Maintenance
    {
        set
        {
            maintenance = value;
        }
        get
        {
            if (maintenance == null)
                return new EntitySet<Maintenance>();

            return maintenance;
        }
    }

    public WriteableBitmap GetPicture()
    {
        using (var memoryStream = new MemoryStream(PicBytes))
        {
            return PictureDecoder.DecodeJpeg(memoryStream);
        }
    }
}

And here's the XAML:

        <ListBox Name="carList" Grid.RowSpan="2" Width="480" SelectionChanged="carList_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="205"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>


                                    <Image Grid.RowSpan="4"
                                           Grid.Column="0"
                                           Source="{Binding PicByte}"
                                           Stretch="Uniform"/>


                                    <TextBlock Text="{Binding Name}"
                                               TextWrapping="Wrap"
                                               Grid.Row="0"
                                               Grid.Column="1"
                                               VerticalAlignment="Stretch"
                                               HorizontalAlignment="Center"
                                               Width="160"/>
                                    <TextBlock Text="{Binding MakeID}"
                                               TextWrapping="Wrap"
                                               Grid.Row="1"
                                               Grid.Column="1"
                                               VerticalAlignment="Stretch"
                                               HorizontalAlignment="Center"
                                               Width="160"/>
                                    <TextBlock Text="{Binding ModelID}"
                                               TextWrapping="Wrap"
                                               Grid.Row="2" 
                                               Grid.Column="1"
                                               VerticalAlignment="Stretch"
                                               HorizontalAlignment="Center"
                                               Width="160"/>
                                    <TextBlock Text="{Binding Year}"
                                               TextWrapping="Wrap"
                                               Grid.Row="3"
                                               Grid.Column="1"
                                               VerticalAlignment="Stretch"
                                               HorizontalAlignment="Center"
                                               Width="160"/>
                                </Grid>
                            </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

My main issue is with the image, how do I call the method GetPicture() from XAML. Or should I do it all with C#?

Edit: I found a way to solve my problem

        try
        {
            var db = MHDatabase.GetDatabase();
            var query = from car in db.Cars
                        join make in db.Makes on car.MakeID equals make.ID
                        join model in db.Model on car.ModelID equals model.ID
                        select new
                        {
                            Name = car.Name,
                            Make = make.Name,
                            Model = model.Name,
                            Picture = car.GetPicture(),
                            Year = car.Year
                        };

            carList.ItemsSource = query;
        }
        catch (Exception)
        {
        }
Julian J. Tejera
  • 1,015
  • 10
  • 17

1 Answers1

0

You can't bind directly to a method. In your case, I see at least two workarounds:

  1. Bind to PicBytes and make a converter to convert from byte array to WriteableBitmap
  2. Create a Picture property:

    public WriteableBitmap Picture
    {
        get
        {
            using (var memoryStream = new MemoryStream(PicBytes))
            {
                return PictureDecoder.DecodeJpeg(memoryStream);
            }
        }
    }
    
Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
  • I thought about it but I didn't want to save the same thing twice. However, I found a way to solve my problem with LINQ. – Julian J. Tejera Dec 24 '12 at 22:11
  • @JulianJ.Tejera Which is basically the "Create a `Picture` property" workaround, you just assign the value of the property differently ;) – Kevin Gosse Dec 25 '12 at 09:34