首页 > 解决方案 > Xamarin Binding 函数作为具有数据绑定的 Listview 中的 ImageSource

问题描述

我试图MinRepresentation在下面的 ListView 中可视化一个名为 的对象的状态。

我想绑定函数ImageSource stateImage(MinRepresentationState state),在那里我切换状态并取回 Imagesource。

我的问题是,访问函数并通过 xaml 传递参数。

Visible OrdersMinRepresentation每个包含的集合State

<ListView HasUnevenRows="True" SelectionMode="Single" ItemsSource="{Binding VisibleOrders}" ItemSelected="OnListViewItemSelected" ItemTapped="OnListViewItemTapped">

***OTHER STUFF***

<StackLayout Orientation="Horizontal" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" Padding= "0,5,0,5" BackgroundColor="#CC3F6E3F">

<Image Source="{Binding stateImage(State)" Margin="10,0,0,0" />

<Label Text="{Binding Id, StringFormat='ID: [{0}]'}" FontSize="Small" Margin="5,0,0,0" FontAttributes="Bold" TextColor="#FFFFFF"/>

</StackLayout>

***OTHER STUFF***

</ListView>
public ImageSource stateImage(MinRepresentationState state)
        {

            switch (state)
            {
                case MinRepresentationState.Assigned:
                    return ImageSource.FromResource("state_assigned.png");
            }
        }

标签: imagelistviewxamarinbinding

解决方案


您只能绑定到公共属性。在模型上创建 StateImage 属性

public string StateImage 
{
  get {
    switch (State)
    {
      case MinRepresentationState.Assigned:
        return "state_assigned.png";
    }
  }
}

推荐阅读