简体   繁体   中英

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.

in My 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#:

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

 <Image Source="{Binding Light}" ></Image>

Can help me :)

实际上, SetBinding方法的第二个参数是属性的名称,而不是属性本身,所以你应该做的是这样的:

dark.SetBinding(Image.SourceProperty, "Light");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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