从视图中使用数据绑定设置背景

时间:2019-07-12 13:00:20

标签: c# android mvvm

我想使用数据绑定来设置Button的背景图像。我可以绑定文本和src图像,但是我用按钮缺少了一些东西...

这就是我现在拥有的。

Viewmodel

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

查看

_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
/>

这已经有效

Viewmodel

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

查看

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" />

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

_openRouteButton = FindViewById(Resource.Id.myId);

_openRouteButton.SetBackgroundResource(Resources.GetIdentifier(ViewModel。VariantBoxGradient,“ drawable”,PackageName));