实现效果:

 

实现遇到的问题:

         当时想要实现如图所示‘合格率’所示的效果,我的第一个想法就是使用wpf的转换器,可是接下来问题来了,我这个是通过数值来判断是否合格,什么控件可以做到既可以绑定图片类型的,又可以绑定数值类型的;还有此时的当值绑定肯定不行,可以多值绑定吗?

 

解决方案:

通过自己的联想,以及网上查询,我终于解决了我的两个疑问,下面我就直奔主题!

  1. “什么控件可以做到既可以绑定图片类型” =》 我选择了Label,这个啥类型的都可以绑定
  2. “多值绑定可以吗” =》 可以,使用“MultiBinding”绑定(对于单值,直接使用Binding)

 

关键代码:

前台

<DataGridTemplateColumn Header="合格率">

                                <DataGridTemplateColumn.CellTemplate>

                                    <DataTemplate>

                                        <Grid>

                                            <!--<Label Content="{Binding Path=hege,Converter={StaticResource dc_departallperson}}"/>-->

                                            <Label>

                                                <Label.Content>

                                                    <MultiBinding Converter="{StaticResource dc_dallpersonicon}">

                                                        <Binding Path="hege"></Binding>

                                                        <Binding Path="userrealname"></Binding>

                                                    </MultiBinding>

                                                </Label.Content>

                                            </Label>

                                        </Grid>

                                    </DataTemplate>

                                </DataGridTemplateColumn.CellTemplate>

                            </DataGridTemplateColumn>

 

<DataGridTextColumn Header="合格率" Binding="{Binding hege,Converter={StaticResource dc_departallperson}}" />

 

转换器中代码:

(通过判断合格率显示对应图片)

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            byte[] img = null;

            if (!string.IsNullOrEmpty(value.ToString()))

            {

                img = (byte[]) value;

                    //从数据库中获取图片数据转换为字节数组(注意:不用用这种方式转换为字节数组,这种转换有问题,我之前一直出不来效果 byte[] img = System.Text.ASCIIEncoding.ASCII.GetBytes(ds.Tables[0].Rows[0]["pic"].ToString()); 现在修改了,就能出来效果了,这个问题还挺让人纠结的呢,所以大家要注意哦!)

            }

            if (img == null)

            {

                return "/HDMSWpfManage;component/templet/images/defaulthead.jpg";

            }

            return ShowSelectedIMG(img);                //以流的方式显示图片的方法

        }

        //转换器中二进制转化为BitmapImage  datagrid绑定仙石的

        private BitmapImage ShowSelectedIMG(byte[] img)

        {

            BitmapImage newBitmapImage = null;

            if (img != null)

            {

                System.IO.MemoryStream ms = new System.IO.MemoryStream(img);//img是从数据库中读取出来的字节数组

                ms.Seek(0, System.IO.SeekOrigin.Begin);

 

                newBitmapImage = new BitmapImage();

                newBitmapImage.BeginInit();

                newBitmapImage.StreamSource = ms;

                newBitmapImage.EndInit();

            }

            return newBitmapImage;

        }

 

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            return null;

        }