Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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
Wpf 将窗口图标绑定到图像_Wpf_Image_Window - Fatal编程技术网

Wpf 将窗口图标绑定到图像

Wpf 将窗口图标绑定到图像,wpf,image,window,Wpf,Image,Window,好的,我有一个自定义窗口控件。我试图做的是设置图像控件,并试图将.Source设置为窗口.Icon属性 我所拥有的是 public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof (ImageSource), typeof (OctgnChrome)); private Image IconImage { get; set; } 在构造函

好的,我有一个自定义窗口控件。我试图做的是设置
图像
控件,并试图将
.Source
设置为
窗口.Icon
属性

我所拥有的是

public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof (ImageSource), typeof (OctgnChrome));
private Image IconImage { get; set; }
在构造函数中

IconImage.SetBinding(IconProperty, new Binding("Icon") {UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged});
octgncrome是自定义窗口的名称

问题是窗口有一个图标,当我运行时它会显示在任务栏上,但是
图像
没有显示任何内容,只是空白

关于如何解决这个问题,或者我可能做错了什么,有什么想法吗

阿尔索 如果我将其设置为直接指向某个图标,它就会工作,如下所示

IconImage = new Image{Source = new BitmapImage(new Uri("pack://application:,,,/Octgn;component/Resources/Icon.ico")) };

我可以看到三个问题

  • 该属性实际上并没有绑定到dependency属性(它使用自己的getter/setter)
  • 属性的命名与您设置的binding+DP注册不匹配,即,它应该命名为
    Icon
    ,而不是
    IconImage
  • 属性的类型与绑定到的属性不同(
    Image
    vs
    ImageSource
  • 我建议像这样实现
    IconImage
    属性:

    public ImageSource Icon 
    { 
        get { return this.GetValue(IconProperty) as ImageSource; }
        set { this.SetValue(IconProperty, value); }
    }
    

    请不要在标题中添加不必要的杂乱内容。平台/技术信息已经通过标签传达,我猜大多数想回答问题的人都是通过标签浏览的,所以他们确切地知道他们当前看到的问题是关于什么的。