tags:

views:

2175

answers:

1

How to bind an image in DataGrid?

 <my:DataGrid.Columns>
            <my:DataGridTemplateColumn Header="状況写真" Width="100">
                <my:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image >
                       ??????????????????
                        </Image>
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellTemplate>
            </my:DataGridTemplateColumn>
   </my:DataGrid.Columns>

Thanks, Vijai

+1  A: 

Image takes its value from the Source property so you need to bind this. Here is an example with ItemsControl.

<ItemsControl Name="imageList">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel></StackPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <!-- The Image binding -->
            <Image Source="{Binding Path=Value}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I used the following code to initialize the items source for the imageList.

List<KeyValuePair<string, string>> images =
    new List<KeyValuePair<string, string>>()
    {
        new KeyValuePair<string,string>("Image1", @"D:\Photos\tn-35.jpg"),
        new KeyValuePair<string,string>("Image2", @"D:\Photos\tn-36.jpg"),
        new KeyValuePair<string,string>("Image3", @"D:\Photos\tn-37.jpg")
    };

imageList.ItemsSource = images;
Mikko Rantanen