Silverlight将图像绑定到整数

时间:2011-03-22 13:09:40

标签: silverlight image datagrid

我这个问题已经很久了。而且我仍然不知道如何解决它...我正在尝试将图像绑定到我的数据网格中的值。

该字段称为Billable,它为0或1.当我有值1时,我想显示一个greenCross。当值为0时,我想显示一个RedCross。

这是代码

<telerik:RadGridView Grid.Column="1" Grid.Row="0" x:Name="RadGridView1" 
                             ItemsSource="{Binding TypeRegs}" 
                             IsReadOnly="True" 
                             DataContext="{Binding Source={StaticResource TypeRegViewModel}}" 
                             AutoGenerateColumns="False" 
                             ScrollMode="Deferred"
                             GridLinesVisibility="Horizontal"
                             IsSynchronizedWithCurrentItem="True" 
                             SelectedItem="{Binding Path=SelectedTypeReg, Mode=TwoWay}"
                             SelectionUnit="FullRow">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn Header="Description" DataMemberBinding="{Binding Description}" />
                    <telerik:GridViewDataColumn Header="Billable" DataMemberBinding="{Binding Billable}" />
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>

有人知道如何解决它吗?

请分步告诉我。我尝试了很多东西......它们都没有用。

THX

2 个答案:

答案 0 :(得分:1)

我刚刚找到答案的人。我会分享它;)

首先,您需要创建一个看起来像这样的转换器。

 public class CommentTypeIconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                int ct = int.Parse(value.ToString());
                if (ct == 1)
                {
                    StreamResourceInfo sr = Application.GetResourceStream(
                        new Uri("/COVR.TimeRegistration;component/Images/true.png", UriKind.Relative));
                    BitmapImage bmp = new BitmapImage();
                    bmp.SetSource(sr.Stream);

                    return bmp;
                }
                else
                {
                    StreamResourceInfo sr = Application.GetResourceStream(
                    new Uri("/COVR.TimeRegistration;component/Images/false.png", UriKind.Relative));
                    BitmapImage bmp = new BitmapImage();
                    bmp.SetSource(sr.Stream);

                    return bmp;
                }
            }
            else
            {
                return MessageBox.Show("Fout");
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

此转换器将返回图像

您的xaml将如下所示

<telerik:RadGridView Grid.Column="1" Grid.Row="0" x:Name="RadGridView1" 
                         ItemsSource="{Binding TypeRegs}" 
                         IsReadOnly="True" 
                         DataContext="{Binding Source={StaticResource TypeRegViewModel}}" 
                         AutoGenerateColumns="False" 
                         ScrollMode="Deferred"
                         GridLinesVisibility="Horizontal"
                         IsSynchronizedWithCurrentItem="True" 
                         SelectedItem="{Binding Path=SelectedTypeReg, Mode=TwoWay}"
                         SelectionUnit="FullRow">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Description" DataMemberBinding="{Binding Description}"/>
                <telerik:GridViewDataColumn Header="Billable" Width="50">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Image Width="16" Height="16" Source="{Binding Billable,Converter={StaticResource CommentTypeIconConverter}}">
                              </Image>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>

这很好用!!

如果您需要任何帮助,可以问:)

答案 1 :(得分:0)

首先,为什么要将其绑定到gridView列,而不是将其直接绑定到Billable属性。也可以使用DataTriggers代替绑定。以下是我在WPF上用于相同目的的代码,请检查它是否可以帮助您使用Silverlight:

<Image>
    <Image.Style>
        <Style TargetType="Image">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Billable}" Value="0">
                    <Setter Property="Source" Value="RedCross.png" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Billable}" Value="1">
                    <Setter Property="Source" Value="GreenCross.png" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>