C# 使用UltraControlContainerEditor嵌入任何控件不适用于图像列

C# 使用UltraControlContainerEditor嵌入任何控件不适用于图像列,c#,winforms,infragistics,C#,Winforms,Infragistics,我想在UltraGridCell中以另一种缩放模式显示图片。该模式应将图像缩放到单元格高度,并从右侧剪裁图像的其余部分以适合单元格。如果我可以自己绘制,这很容易,因为EmbeddedBaleImageRenderer不允许我设置其缩放行为(我不是说MaintaintAspectRatio,因为我仍然希望保持纵横比) 我试过了。在给定的TrackBar示例中,它工作得很好(在我的小型测试项目中,ProgressBar也作为rendercontrol)。但它似乎不适用于显示图像的列 作为一个数据源,

我想在UltraGridCell中以另一种缩放模式显示图片。该模式应将图像缩放到单元格高度,并从右侧剪裁图像的其余部分以适合单元格。如果我可以自己绘制,这很容易,因为EmbeddedBaleImageRenderer不允许我设置其缩放行为(我不是说MaintaintAspectRatio,因为我仍然希望保持纵横比)

我试过了。在给定的TrackBar示例中,它工作得很好(在我的小型测试项目中,ProgressBar也作为rendercontrol)。但它似乎不适用于显示图像的列

作为一个数据源,我有一个我自己的类的列表,其中包含一个在网格中显示的Image属性。作为编辑器-/renderControl,我设置了两个常规的PictureBox


有什么建议可以解决缩放图像的主要问题,或者为图片列设置任何控件(这样可以处理缩放问题)?

我看不出UltraControlContainerEditor为什么不能处理图像列,前提是您使用的控件具有获取图像的属性,并且您在编辑器上指定了正确的PropertyName。但无论如何,这可能不是最有效的方法

更好的方法是使用DrawFilter自己绘制图像

public class ImageScalingDrawFilter : IUIElementDrawFilter
{
    bool IUIElementDrawFilter.DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams)
    {
        switch (drawPhase)
        {
            case DrawPhase.BeforeDrawImage:

                ImageUIElement imageElement = (ImageUIElement)drawParams.Element;
                Image image = imageElement.Image;                    

                int availableHeight = drawParams.Element.RectInsideBorders.Height;
                float ratio = (float)availableHeight / (float)image.Height;

                float newHeight = image.Height * ratio;
                float newWidth = image.Width * ratio;

                Rectangle rect = new Rectangle(
                    imageElement.Rect.X,
                    imageElement.Rect.Y,
                    (int)(newWidth),
                    (int)(newHeight)

                    );

                // Draw the scaled image.
                drawParams.Graphics.DrawImage(image, rect);

                // This tells the grid not to draw the image (since we've already drawn it). 
                return true;
        }

        return false;
    }

    DrawPhase IUIElementDrawFilter.GetPhasesToFilter(ref UIElementDrawParams drawParams)
    {
        UIElement element = drawParams.Element;
        // Look for an ImageUIElement
        if (element is ImageUIElement)
        {
            // Presumably, we only want to this images in cells
            // and not every image in the entire grid, so make sure it's in a cell.
            CellUIElement cellElement = element.GetAncestor(typeof(CellUIElement)) as CellUIElement;
            if (null != cellElement)
            {
                // We could also limit this to a particular column or columns.
                switch (cellElement.Cell.Column.Key)
                {
                    case "Image":
                        return DrawPhase.BeforeDrawImage;
                }
            }
        }

        return DrawPhase.None;
    }
}
将DrawFilter指定给网格,如下所示:

    private void Form1_Load(object sender, EventArgs e)
    {
        this.ultraGrid1.DrawFilter = new ImageScalingDrawFilter();
    }

虽然您的答案对于问题的时间来说已经很晚了,但我仍然对此表示感谢,我会尽快尝试,因为您的代码看起来很有希望。我想我会在2个月内再次接触这个代码。我将再次发表评论,让任何人知道这是否是一个好的选择。