开发者

Datagrid binding 2 datafiled under one column along with an image?

here i am performing nested grid operation where in my first datagrid . have name,age,address

once user clicks the row we will be showing another grid that is with data Subject name and Score

untill here the functionality works fine .

1:now the Question is that i should be displaying an image along with score in the same column( how can i achive this one)

2:Depending on the score i need to show an image( binding different image based on score)

1 :that is if score is 20 show ( ~images/image1.jpg)

2 :that is if score is 40 show ( ~images/image2.jpg)

3 :that is if score is 20 show (~images/ image3.jpg)

output [ in a grid  forma开发者_如何学运维t]
 subject score  
 science  45 image3
 maths    50 image2
 Computer 60 image1
 general  78 image1

how do we solve this issue . any idea how to go ahead. please let me know. hope my question is clear

thanks in advance

prince


Since you tagged this question with Silverlight v4.0, I will assume that you are trying to accomplish this in said technology. One way to accommplish this would be to add a converter as a static resource and then use said converter to pull in the data. For all intensive purposes, I am assuming that the image files are named with in the format imageX.jpg, where X is equal to the score, but you can use whatever format you prefer. Please see below a untested, sample of XAML and C# code:

<USERCONTROL.RESOURCES>
    <UserControl.Resources> 
        <SvcTest:DSTest x:Key="DSTest" d:IsDataSource="True"/> 
<local:IDToImageConverter x:Key="IDToImageConverter"/> 
</UserControl.Resources>
<UserControl.Resources> 
<StackPanel x:Name="OurStack" Orientation="Vertical"> 
    <TextBox x:Name="InputText"/> 
    <TextBlock x:Name="OutputText"/> 
    <Button x:Name="CallServiceButton" Content="Call WCF" Click="CallServiceButton_Click"/> 
    <data:DataGrid x:Name="theDataGrid" AlternatingRowBackground="Beige" Grid.Row="2" Grid.Column="1" Height=" 600" Width="800" CanUserResizeColumns="True" AutoGenerateColumns="False"> 
        <data:DataGrid.Columns> 
            <data:DataGridTextColumn Header="InventTypeID" Width="100"  Binding="{Binding InventTypeID}" /> 
<data:DataGridTemplateColumn Header="ImageColumn" DisplayIndex="1" > 
<data:DataGridTemplateColumn.CellTemplate> 
<DataTemplate> 

<Image Source="{Binding ItemID, Converter={StaticResource IDToImageConverter}}" Height="50" /> 
</DataTemplate> 
</data:DataGridTemplateColumn.CellTemplate> 
</data:DataGridTemplateColumn> 
</data:DataGrid.Columns> 
</data:DataGrid> 
</StackPanel>
<?xml:namespace prefix = local /><local:IDToImageConverter x:Key="IDToImageConverter"></local:IDToImageConverter>
</USERCONTROL.RESOURCES>

    public class IDToImageConverter : IValueConverter 
    { 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
    Uri uri = new Uri("~/Images/" + value.ToString()+ ".jpg", UriKind.Relative); 
    return new BitmapImage(uri); 
    }

<GRID Background="White" x:Name="LayoutRoot">
<STACKPANEL x:Name="OurStack" Orientation="Vertical">
<TEXTBOX x:Name="InputText" />
<TEXTBLOCK x:Name="OutputText" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜