简体   繁体   中英

Set backgrund with Data-Binding from view

I want to use databinding to set background image of a Button. I can bind Text and src images but im missing something with buttons...

This i what I have now..

Viewmodel

public string VariantBoxGradient { get; set; } = 'imageName';

View

_openRouteButton = FindViewById<ButtonOpacity>(Resource.Id.myId);

BindingSet.Bind(_openRouteButton).For(x => x.Background).To(vm => vm.VariantBoxGradient);

axml

<ButtonOpacity
    android:id="@+id/myId"
    ...
    // android:background="@drawable/imageName" // This i want to remove
/>

This is what already works

Viewmodel

public string VariantPic1 { get; set; } = 'step1_image';
public string TextDescriptionPic1 => string.Format('Something');

View

var infoPic1 = FindViewById<ImageView>(Resource.Id.info_pic1);
var descriptionPic1 = FindViewById<TextView>(Resource.Id.info_description_pic1);

BindingSet.Bind(infoPic1).For("DrawableName").To(vm => vm.VariantPic1);
BindingSet.Bind(descriptionPic1).For(x => x.Text).To(vm => vm.TextDescriptionPic1);

axml

<ImageView
    android:id="@+id/info_pic1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/info_description_pic1"
    android:textSize="12sp"
    android:gravity="left" />

I solved it doing this:

_openRouteButton = FindViewById(Resource.Id.myId);

_openRouteButton.SetBackgroundResource(Resources.GetIdentifier(ViewModel. VariantBoxGradient, "drawable", PackageName));

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