I added a GridImageColumn to my grid and am adding an image to it. It seems that the width of the column (when I set the ColumnSizer="Auto") is being set to the length of the url I am dl'ing the image from, and not the actual image size. If I use some really short url, the column's width is also really small.
Is this expected? Do I need to just explicitly set the width of the column? What I want is for the column to automatically be the width of the widest image in the column.
Here is the grid:
<syncfusion:SfDataGrid x:Name="dataGrid" ItemsSource="{Binding TestItems}" ColumnSizer="Auto">
<syncfusion:SfDataGrid.Columns>
<syncfusion:GridImageColumn
MappingName="TEST"
HeaderText="TEST"
Stretch="None"
TextAlignment="Center"
ValueBinding="{Binding Path=ImagePath, Converter={StaticResource StringToImageConverter}}" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>Here is StringToImageConverter. It just fetches the BitmapImage and returns it:
class StringToImageConverter : IValueConverter
{
private const string UriPrefix = "https://www.someprefix.com";
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
string imageName = value is string ? UriPrefix + value : string.Empty;
return new BitmapImage(new Uri(imageName, UriKind.RelativeOrAbsolute));
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return null;
}
}And it comes out looking like this (the image has a width of 112 and the url is pretty long, basically as wide as the column is in the below screenshot):
Note that I am able to reproduce the issue using http://www.syncfusion.com/downloads/support/directtrac/215737/ze/GridDropDownAndReadOnlyColumns27872867.zip.
To reproduce, replace the orderInfo.ImageLink properties with @"https://ffrk.denagames.com/dff/static/lang/image/sphere_material/131008300/131008300_112.png" and in StringToImageConverter replace UriKind.Relative with UriKind.RelativeOrAbsolute.
I am on Syncfusion 16.2.0.50.
|
this.AssociatedObject.GridColumnSizer = new CustomGridColumnSizer(this.AssociatedObject);
public class CustomGridColumnSizer : GridColumnSizer
{
public CustomGridColumnSizer(SfDataGrid grid) : base(grid)
{
}
protected override Size GetCellSize(Size rect, GridColumn column, object data, GridQueryBounds bounds)
{
if (column is GridImageColumn)
{
var orderinfo = data as OrderInfo;
if (orderinfo != null)
{
var bitmapImage = new BitmapImage(new Uri(orderinfo.ImageLink));
byte[] imageData = new WebClient().DownloadData(orderinfo.ImageLink);
MemoryStream imgStream = new MemoryStream(imageData);
System.Drawing.Image img = System.Drawing.Image.FromStream(imgStream);
int wSize = img.Width;
int hSize = img.Height;
return new Size(wSize, hSize);
}
}
return base.GetCellSize(rect, column, data, bounds);
}
}
} |
|
<syncfusion:SfDataGrid AllowEditing="True" x:Name="dataGrid"
AutoGenerateColumns="False"
RowHeight="122"
ItemsSource="{Binding OrderList}"
ShowRowHeader="True">
|
|
this.AssociatedObject.QueryRowHeight += AssociatedObject_QueryRowHeight;
private void AssociatedObject_QueryRowHeight(object sender, QueryRowHeightEventArgs e)
{
double height = this.AssociatedObject.RowHeight;
if (this.AssociatedObject.GridColumnSizer.GetAutoRowHeight(e.RowIndex, new GridRowSizingOptions(), out height))
{
if (height > this.AssociatedObject.RowHeight)
e.Height = height;
else
e.Height = this.AssociatedObject.RowHeight;
e.Handled = true;
}
} |