[英]How To Bind Image Source in C# (MVVM)
In My Project,I Create Some Images with C# and i want these photo's sources, Bind to a property in My ViewModel. 在我的项目中,我使用C#创建一些图像,我想要这些照片的来源,绑定到My ViewModel中的属性。
in My MVVM : 在我的MVVM中:
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private string _Light= "dark.png";
public string Light
{
get { return _Light; }
set {
_Light = value;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(Light)));
}
}
in My C#: 在我的C#中:
BindingContext = new LightViewModel();
LightViewModel light = new LightViewModel();
Image dark = new Image { Margin = new Thickness(0, -5, 0, 10), HeightRequest = 20, WidthRequest = 20 };
dark.SetBinding(Image.SourceProperty, light.Light);
i use exactly this MVVM with this Xaml, and it's Property work 我使用这个Xaml完全使用这个MVVM,它的属性工作
<Image Source="{Binding Light}" ></Image>
Can help me :) 可以帮我 :)
实际上, SetBinding
方法的第二个参数是属性的名称,而不是属性本身,所以你应该做的是这样的:
dark.SetBinding(Image.SourceProperty, "Light");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.