1

我正在尝试将 DataTemplate 中的 Image.Source 绑定到 System.Drawing.Image,如下所述:使用 XAML 将 System.Drawing.Image 绑定到 System.Windows.Image 控件

<UserControl.Resources>
    <media:ImageConverter x:Key="imageConverter" />
    <DataTemplate DataType="{x:Type data:GameTile}" >
        <StackPanel Orientation="Vertical" Margin="5" Background="Transparent">
            <Viewbox>
                <TextBlock FontWeight="Bold" Text="{Binding PointValue}" TextAlignment="Center" FontSize="14" />
            </Viewbox>
            <Image Margin="0,5,0,0" Source="{Binding Path=Image.Image, Converter={StaticResource imageConverter}}" />
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>
    <Grid>
        <loop:ListBox x:Name="listBox1" 
                         ItemsSource="{Binding Path=GameModel.Game.GameTiles}"
                         ItemContainerStyle="{StaticResource GameTileContainerStyle}" Orientation="Vertical" />
    </Grid>

GameTile 对象具有指向具有 System.Drawing.Image 类型的 Image 属性的 Picture 对象的 Image(不是 system.drawing.image)属性。我将 ListBox 上的 ItemsSource 绑定到 Game 对象上的 GameTiles 集合。

对象

public class Game
{
    public XPCollection<GameTile> GameTiles
    {
        get { return GetCollection<GameTile>("GameTiles"); }
    }
}

public class GameTiles
{
    Picture fImage;
    public Picture Image
    {
        get { return fImage; }
        set { SetPropertyValue<Picture>("Image", ref fImage, value); }
    }
}

public class Picture
{

    private FileData fFile;
    public FileData File
    {
        get { return fFile; }
        set
        {
            SetPropertyValue("File", ref fFile, value);
            if (string.IsNullOrEmpty(fName))
            {
                fName = (value == null ? string.Empty : value.FileName);
            }
            fImage = null;
        }
    }

    Image fImage;
    public System.Drawing.Image Image
    {
        get 
        {
            if (fImage == null)
            {
                try
                {
                    MemoryStream stream = new MemoryStream();
                    fFile.SaveToStream(stream);
                    stream.Position = 0;
                    fImage = Image.FromStream(stream);
                }
                catch 
                { 
                    //TODO: log exception
                }
            }
            return fImage;
        }
        //set { SetPropertyValue<Image>("Image", ref fImage, value); }
    }
}

图像未显示在 ListBoxItems 中,但我在 DataTemplate 中绑定的任何其他属性都会显示。值得注意的是,我使用 Devexpress Xpo 作为 ORM。上面表示的类也实现了 INotifyPropertyChanged。关于我可能遗漏的任何想法?

编辑:忘了提到我已经实现了一个价值转换器,正如我在上面链接的帖子中提到的那样。但是,如果我在转换器方法中放置一个断点,它就永远不会被调用。

编辑:在上面的代码中添加了 fFile 属性。我可以通过 c#(通过将其转换为 BitmapImage)将 Image.Source 设置为 GameTile.Image.Image 属性,并使其按预期工作,但我不确定如何通过 c# 使用 DataTemplate 来完成此操作。我更愿意在 XAML 中设置绑定,但会满足于使用 DataTemplate(或其他可行的方法)的 ac# 解决方法。我非常确信问题不在于从数据库中提取图像的 GameTile.Image 属性,因为如果我在 c# 中手动设置 Image.Source 上的源,则图像就在那里。它根本无法在 DataTemplate 中工作。

编辑:确定问题与不直接在我绑定到的 DataType 上的属性相关,例如,GameTile 具有 (int)PointValue 属性、(Picture object)Image 属性和 (Prize object)Prize财产。

如果我绑定到

<TextBlock Text="{Binding PointValue}" /> 

它按预期工作。但是如果我绑定到

<TextBlock Text="{Binding Prize.Name}" /> 

这没用。如果我绑定到

<Image Margin="0,5,0,0" Source="{Binding Image.BitmapImage}" />

它也失败了。下图显示了绑定引发的错误。

BindingExpression 路径错误:在“对象”“XPCollection”(哈希=...)上找不到“名称”属性。BindingExpression:Path=Prize.Name; DataItem=GameTile' (HashCode=...); 目标元素是 'TextBlock'(Name=''); 目标属性是“文本”(类型“字符串”)

在此处输入图像描述

谢谢,埃里克

4

1 回答 1

1

找到了解决方案。不得不改变这个:

<TextBlock Text="{Binding Prize.Name}" /> 

对此:

<TextBlock Text="{Binding Prize!.Name}" /> 

唯一的区别是感叹号(!)。这也适用于图像属性。

<Image Margin="0,5,0,0" Source="{Binding Path=Image!.Image, Converter={StaticResource imageConverter}}" />
于 2012-11-06T15:28:44.120 回答