如何将图像绑定到GridColumn? [英] How to bind image to GridColumn?

查看:141
本文介绍了如何将图像绑定到GridColumn?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有FieldName == Image的GridColumn。图像是MyClass中BitmapImage的一种属性类型,该属性类型是在构造函数中分配的。

I have a GridColumn with FieldName=="Image". Image is a property type of BitmapImage in MyClass, which is assigned in constructor.

XAML:

<dxg:GridColumn  Header="MyImage" ReadOnly="True" VisibleIndex="0" AllowResizing="False" Width="20*"
                HorizontalHeaderContentAlignment="Center"
                FieldName="Image">    
    <dxg:GridColumn.EditSettings>
    <dxe:ImageEditSettings MaxWidth="15" />
</dxg:GridColumn.EditSettings></dxg:GridColumn>

MyClass:

public class MyClass
{
    public MyClass(ImageType imageType)
    {
        Image = imageType switch
        {
            ImageType.T1=> new BitmapImage(new Uri(@"pack://application:,,,/MyProject;component/Assets/Images/information-blue-red.png", UriKind.RelativeOrAbsolute)),
            ImageType.T2=> new BitmapImage(new Uri(@"pack://application:,,,/MyProject;component/Assets/Images/information-blue.png", UriKind.RelativeOrAbsolute)),
            ImageType.T3=> new BitmapImage(new Uri(@"pack://application:,,,MyProject;component/Assets/Images/information-red.png", UriKind.RelativeOrAbsolute)),
            ImageType.T4=> new BitmapImage(new Uri(@"pack://application:,,,/MyProject;component/Assets/Images/information-white.png", UriKind.RelativeOrAbsolute)),
            _ => default
        };
    }

    public BitmapImage Image { get; set; }
}

所以我用该类型的ItemsSource填充了GridControl。当我执行程序时-首先会调用刷新方法,并且一切正常,这意味着每个单元格都包含需要的图像。但是,如果我再次刷新它(调用这种方法,这是异步的)-再次填充ItemsSource,并且创建MyClass对象没有任何问题,但是我遇到一个错误,那就是对象在另一个线程中,因此刷新后无法访问。我不确切知道哪个对象,但是我敢肯定,这与Image属性有关,因为我已经在没有此类列的情况下进行了测试,结果还可以。

So I fill GridControl with ItemsSource of that type. When I execute the program - refreshing method is called at first and everything is OK, I mean that each cell contains needing image. But if I refresh it again(calling such method, which is asynchronous) - ItemsSource is being filled again, and MyClass objects are being created without any problems, but I'm getting an error, that an object is in another thread, so it is inaccessible, after refreshing. I don't know exactly, which object, but I'm sure, that's related to Image property, because I've tested that without such column, and result was OK.

错误:

System.InvalidOperationException:调用线程无法访问该对象,因为另一个线程拥有此对象。

System.InvalidOperationException: "The calling thread cannot access this object because another thread owns this object."

推荐答案

您的MyClass构造函数似乎在UI线程之外的其他线程中调用。因此,应该注意通过冻结它来使Image属性中的BitmapImage跨线程可访问。

Your MyClass constructor seems to be called in a thread other than the UI thread. It should hence take care for making the BitmapImage in the Image property cross-thread accessible by freezing it.

该属性也应该是只读的,或者在设置该属性时应发出更改通知

The property should also be readonly, or it should fire a change notification when it is set.

,由于Pack URI始终是绝对的,因此无需设置 UriKind.RelativeOrAbsolute

And there is no need to set UriKind.RelativeOrAbsolute because a Pack URI is always absolute.

public MyClass(ImageType imageType)
{
    var name = imageType switch
    {
        ImageType.T1 => "information-blue-red.png",
        ImageType.T2 => "information-blue.png",
        ImageType.T3 => "information-red.png",
        ImageType.T4 => "information-white.png",
        _ => null
    };

    if (name != null)
    {
        var uri = new Uri(
            "pack://application:,,,/MyProject;component/Assets/Images/" + name);

        var image = new BitmapImage(uri);
        image.Freeze(); // here
        Image = image;

        // alternatively, call Image = BitmapFrame.Create(uri);
    }
}

public ImageSource Image { get; }

这篇关于如何将图像绑定到GridColumn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆