简体   繁体   English

如何以编程方式设置Xaml定义的Window.Resource Image?

[英]How do you set a Xaml defined, Window.Resource Image programmatically?

I have a xaml file that has a series of resources that I use in my window. 我有一个xaml文件,其中包含我在窗口中使用的一系列资源。 Specifically, I have two Image objects. 具体来说,我有两个Image对象。 Instead of encoding a URI location, I need to set these images in the constructor of my Window. 我需要在Window的构造函数中设置这些图像,而不是对URI位置进行编码。 How can I do this? 我怎样才能做到这一点?

I've searched and searched, and I've tried to set the image .Source property. 我进行了搜索,并尝试设置image .Source属性。 Nothing has worked. 没事。 For reference, here is how these images are declared in my xaml file: 供参考,这是在我的xaml文件中声明这些图像的方式:

<Window.Resources>
    <Image x:Key="iUpdate" />
    <Image x:Key="iDelete" />
</Window.Resources>

And here is how they are used for a button's content: 以下是它们用于按钮内容的方式:

                <DataTemplate>
                    <DockPanel>
                        <Button Name="UpdateChange" Content="{StaticResource iUpdate}" Width="24" />
                        <Button Name="DeleteChange" Content="{StaticResource iDelete}" Width="24" />
                        <Label Content="{Binding name}" />
                    </DockPanel>
                </DataTemplate>

Here is my failed attempt at setting the image's source. 这是我设置图像来源失败的尝试。

        //In my constructor...
        // iUpdate is a property on the Window
        iUpdate = (Image)FindResource("iUpdate");
                    iUpdate = new Image();
        iUpdate.Source = ImageHelpers.ConvertBitmapToImageSource(Common.LoadImage("edit.ico"));

FYI, I can't set the source in my xaml file because the call to Common.LoadImage(...) is a special call that resolves the location of the file. 仅供参考,我无法在我的xaml文件中设置源,因为对Common.LoadImage(...)的调用是一个特殊的调用,它可以解析文件的位置。 This could vary significantly from user-to-user, which is why the helper-method exists in the first place. 各个用户之间的差异可能很大,这就是为什么首先要使用辅助方法的原因。

Instead of using Image controls as resources, you could declare and bind to two properties in your MainWindow class. 您可以在MainWindow类中声明并绑定到两个属性,而不是将Image控件用作资源。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        UpdateImage = ImageHelpers.ConvertBitmapToImageSource(...);
        DeleteImage = ImageHelpers.ConvertBitmapToImageSource(...);

        InitializeComponent();
    }

    public ImageSource UpdateImage { get; set; }
    public ImageSource DeleteImage { get; set; }
}

You would bind to these properties by assigning a Name to the Window 您可以通过为窗口分配名称来绑定到这些属性。

<Window ... x:Name="window">

and declare an ElementName binding: 并声明一个ElementName绑定:

<Button ...>
    <Button.Content>
        <Image Source="{Binding UpdateImage, ElementName=window}"/>
    </Button.Content>
</Button>

And to answer your question, your problem is that you assign a new Image instance to the iUpdate field right after assigning it to the resource instance. 为了回答您的问题,您的问题是,在将新的Image实例分配给资源实例之后,立即将其分配给了iUpdate字段。 Your code should have looked like this: 您的代码应该看起来像这样:

iUpdate = (Image)Resources["iUpdate"];
iUpdate.Source = ImageHelpers.ConvertBitmapToImageSource(...);

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

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