gpt4 book ai didi

c# - 如何绑定(bind)到静态 BitmapSource?

转载 作者:行者123 更新时间:2023-11-30 22:20:23 25 4
gpt4 key购买 nike

我想使用标准 Windows“错误图标”创建自定义错误/异常对话框。

我听从了 this question 的建议并且有效。

但是我目前正在创建一个实例属性,我绑定(bind)到它就像绑定(bind)到任何属性一样:

class ErrorWindowViewModel
{
private readonly ImageSource _errorImage;

public ImageSource ErrorImage { get { return _errorImage; } }

public ErrorWindowViewModel()
{
_errorImage = Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
}

我想做的是在我的 ErrorWindow 类中定义一个静态字段:

partial class ErrorWindow : Window
{
private readonly static ImageSource ErrorImage =
Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}

我不能强制我的 XAML 引用该字段。

<Image Source="what_to_put_here_to_make_it_work" />

我正在使用 WPF 4.5。

最佳答案

你必须创建一个静态属性

private static readonly ImageSource errorImage =
Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

public static ImageSource ErrorImage
{
get { return errorImage; }
}

然后像这样绑定(bind):

<Image Source="{Binding Source={x:Static local:ErrorWindow.ErrorImage}}"/>

关于c# - 如何绑定(bind)到静态 BitmapSource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15032298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com