关于Image 控件绑定的bug

卿 贾 0 信誉分
2026-02-26T10:56:59.6233333+00:00

代码通过 xml 文本生成控件后 再生产 xml 文本,绑定会变成

<Image Source="{x:Null}"/>

代码如下

示例1

string xaml = $@" <Image Source='{{Binding Icon}}' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'/>";

         var image=(Image)XamlReader.Parse(xaml);

            image.Width = 24;

            image.Height = 24;

            image.Margin = margin;

            var xml= XamlWriter.Save(image);

示例2

       Image image = new Image();

       var bind = new Binding("Icon")

       {

           NotifyOnValidationError = true,

       };

       image.SetBinding(Image.SourceProperty, bind);

           var xml= XamlWriter.Save(image);
开发人员技术 | Windows Presentation Foundation
0 个注释 无注释
{count} 票

你的答案

提问者可以将答案标记为“已接受”,审查方可以将答案标记为“已推荐”,这有助于用户了解答案是否解决了提问者的问题。