简体   繁体   English

WPF BitmapImage 不显示

[英]WPF BitmapImage not displaying

I have a WPF project which uses, as best I can, the MVVM pattern.我有一个 WPF 项目,它尽可能地使用 MVVM 模式。 I'm using Visual Studio 2010, and the language behind is C#.我使用的是 Visual Studio 2010,背后的语言是 C#。

So I have a treeview.所以我有一个树视图。 The treeview looks like this:树视图如下所示:

<TreeView Grid.Column="0" Width="Auto" Background="Aquamarine" ItemsSource="{Binding Scenes}" SelectedItemChanged="TreeView_SelectedItemChanged">
        <TreeView.DataContext>
            <viewModels:ScenesViewModel />
        </TreeView.DataContext>
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Characters}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding SceneName}"></TextBlock>

                        <Image Source="{StaticResource ImgWorld}" Margin="0,0,5,0" Width="32" Height="32"/>

                </StackPanel>
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">

                            <TextBlock Text="{Binding FirstName}"></TextBlock>
                            <Border BorderThickness="1" Background="RoyalBlue">
                                <Image Source="{Binding ImgIcon}" Margin="2"/>
                            </Border>
                        </StackPanel>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

The point of contention is the Image, the source of which is bound to something called 'ImgIcon'.争论的焦点是图像,其来源绑定到称为“ImgIcon”的东西。 This is a property of a view model called 'CharacterViewModel'.这是名为“CharacterViewModel”的视图模型的属性。 For some reason I am getting nothing displayed where the image should be.出于某种原因,我没有在图像应该显示的位置显示任何内容。

The most obvious issue might be regarding the data context or whether the MVVM side of things is working.最明显的问题可能是关于数据上下文或 MVVM 方面是否正常工作。 I am fairly sure it is, as there is a TextBlock next to the Image which is also bound to a property called 'FirstName'.我相当确定它是,因为 Image 旁边有一个 TextBlock,它也绑定到一个名为“FirstName”的属性。 This binding works fine.这种绑定工作正常。

The two properties are, of course, in the view model, and can be seen here:这两个属性当然在视图模型中,可以在这里看到:

public String FirstName
    {
        get { return Character.FirstName; }
        set
        {
            if (Character.FirstName != value)
            {
                Character.FirstName = value;
                RaisePropertyChanged("FirstName");
            }
        }
    }

private BitmapImage _imgIcon = null;
    public BitmapImage ImgIcon 
    { 
        get { return _imgIcon; }
        set 
        {
            _imgIcon = value;
            RaisePropertyChanged("ImgIcon");
        }
    } 

They are slightly different in that the latter does not wrap around a property in the Character class.它们略有不同,后者不包含 Character 类中的属性。

So the next possible fault would be the loading of the image.所以下一个可能的错误是图像的加载。 This is accomplished by this method (which at the moment just has a test path for a known working file):这是通过这种方法完成的(目前它只有一个已知工作文件的测试路径):

public bool LoadIcon()
    {
        if (ImgIcon == null)
        {
            Uri outUri;

            if (Uri.TryCreate(@"D:\Pictures\Icons\HCAria01.png", UriKind.Absolute, out outUri) )
            {
            }


            ImgIcon = new BitmapImage(outUri);
            return true;
        }
        else
            return false;
    }

I put a break point in at 'ImgIcon = new BitmapImage(outUri)' and you can see the relevant information here: picture .我在 'ImgIcon = new BitmapImage(outUri)' 放了一个断点,你可以在这里看到相关信息:图片 It seems from this picture that the image has loaded successfully.从这张图片看来,图片已经成功加载了。

I'm sure I'm missing something obvious, but I'm damned if I can say what it is.我确定我遗漏了一些明显的东西,但我该死的,如果我能说出它是什么。 From my limited experience with WPF and XAML, it seems to be working okay, except for the lack of an image.从我对 WPF 和 XAML 的有限经验来看,它似乎工作正常,除了缺少图像。

Well, if you can offer any help that would be much appreciated.好吧,如果您能提供任何帮助,将不胜感激。

Edit : a couple of clarifications.编辑:一些说明。 The DataContext for the Treeview is called ScenesViewModel. Treeview 的 DataContext 称为 ScenesViewModel。 This holds a list of SceneViewModel, and that list is called 'Scenes'.这包含一个 SceneViewModel 列表,该列表称为“场景”。 Each SceneViewModel containst a list of CharacterViewModel, and that is called 'Characters'.每个 SceneViewModel 包含一个 CharacterViewModel 列表,称为“Characters”。 These two images show the relevant part of those classes, so hopefully the hierarchy can be understood.这两个图像显示了这些类的相关部分,因此希望可以理解层次结构。 For each CharacterViewModel, there should be an ImgIcon.对于每个 CharacterViewModel,都应该有一个 ImgIcon。

Make sure that you bind to the view model ( ScenesViewModel ) property if the ImgIcon property is defined in this class:如果ImgIcon属性在此类中定义,请确保绑定到视图模型 ( ScenesViewModel ) 属性:

<Image Source="{Binding DataContext.ImgIcon, RelativeSource={RelativeSource AncestorType=TreeView}}" Margin="2"/>

The DataContext of the StackPanel in the ItemTemplate is an item in the ItemsSource and not the view model. ItemTemplate StackPanelDataContextItemsSource一个项目,而不是视图模型。

Edit: You should define one template for SceneViewModel objects and another one for CharacterViewModel objects.编辑:您应该为 SceneViewModel 对象定义一个模板,为 CharacterViewModel 对象定义另一个模板。 You could put the implicit templates in the section of the TreeView:您可以将隐式模板放在 TreeView 的部分中:

<TreeView Grid.Column="0" Width="Auto" Background="Aquamarine" ItemsSource="{Binding Scenes}" 
                  SelectedItemChanged="TreeView_SelectedItemChanged">
    <TreeView.DataContext>
        <viewModels:ScenesViewModel />
    </TreeView.DataContext>
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:SceneViewModel}" ItemsSource="{Binding Characters}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding SceneName}"></TextBlock>
                <Image Source="{StaticResource ImgWorld}" Margin="0,0,5,0" Width="32" Height="32"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type local:CharacterViewModel}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding FirstName}"></TextBlock>
                <Border BorderThickness="1" Background="RoyalBlue">
                    <Image Source="{Binding ImgIcon}" Margin="2"/>
                </Border>
            </StackPanel>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

This answer is for those looking to see what happened - maybe you have the same issue.这个答案是为那些想看看发生了什么的人准备的——也许你有同样的问题。

My problem was actually elsewhere in my code.我的问题实际上在我的代码中的其他地方。 When I loaded a Scene (data model) from my database to populate the SceneViewModel's data, I created one instance which I used to load the image, and then added a new instance to the list.当我从数据库加载场景(数据模型)以填充 SceneViewModel 的数据时,我创建了一个用于加载图像的实例,然后向列表中添加了一个新实例。 So there were two instances, one which was created, used to load the images and then not used, and another which was used (but which didn't have the images loaded).所以有两个实例,一个被创建,用于加载图像,然后没有使用,另一个被使用(但没有加载图像)。 Basically it was a simple mistake and not related to the code seen here - that all worked fine (since now that I corrected the error, things work as they should, images and all).基本上,这是一个简单的错误,与此处看到的代码无关 - 一切正常(自从我纠正了错误以来,一切都按预期工作,图像等等)。

Thanks to all those who took the time to answer.感谢所有花时间回答的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM