Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF:将位图源绑定到XAML资源失败_C#_Wpf_Xaml - Fatal编程技术网

C# WPF:将位图源绑定到XAML资源失败

C# WPF:将位图源绑定到XAML资源失败,c#,wpf,xaml,C#,Wpf,Xaml,我试图在新窗口中显示由OpenFileDialog加载的图片。 第一种方法是完全在代码中工作。不过,我想用另一种方式。我尝试将该图像添加到资源中,然后将其用于给定的控件 XAML部分: <Window.Resources> <Image x:Key="imageResource"> <Image.Source> <BitmapImage UriSource="{Binding ImageUri}" />

我试图在新窗口中显示由
OpenFileDialog
加载的图片。 第一种方法是完全在代码中工作。不过,我想用另一种方式。我尝试将该图像添加到资源中,然后将其用于给定的控件

XAML部分:

<Window.Resources>
    <Image x:Key="imageResource">
        <Image.Source>
            <BitmapImage UriSource="{Binding ImageUri}" />
        </Image.Source>
    </Image>
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Image Name="image"  />
    <Image Name="image2" Grid.Column="1" Source="{Binding imageResource}" />
</Grid>
左列(在c#代码中完成)显示加载的图片,但右列不显示(使用绑定的图片)。
另外,关于在新窗口中打开每个新图片,我意识到
ImageUri
更改通知在这里是多余的。请忽略此项。

删除图像资源并直接绑定到窗口的
ImageUri
属性:

<Image Name="image2" Grid.Column="1"
       Source="{Binding ImageUri, RelativeSource={RelativeSource AncestorType=Window}}" />


内置类型转换将自动从类型
Uri
转换为
ImageSource

我可以工作,但我想知道如何将我的图像添加到
窗口引用中,将图像元素作为资源是没有意义的。但是,您可以将BitmapImage声明为资源。
<Image Name="image2" Grid.Column="1"
       Source="{Binding ImageUri, RelativeSource={RelativeSource AncestorType=Window}}" />