c# - XAML 绑定(bind)到标签

标签 c# xaml xamarin.forms

在 Xamarin.Forms 中,我有一个用于显示数据库项目的页面。

public ListItemsPage()
{
    DatabaseHelper tmpDBHelper = new DatabaseHelper();
    InitializeComponent();

    listView.ItemsSource = tmpDBHelper.GetItems();
}

这些项目有 4 列:Id 为 Guid、MandantId 为 Guid、UpdateAt 为 DateTime 和 Url 为 String.

现在我想像这样将它们绑定(bind)在我的 XAML 中。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Portable.Pages.ListItemsPage"
             Title="DbItemExample">
  <ListView x:Name="listView">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <ViewCell.View>
            <StackLayout Padding="20,0,20,0"
                         Orientation="Horizontal"
                         HorizontalOptions="FillAndExpand">

              <Label Text="{Binding Id}"
                     HorizontalOptions="StartAndExpand" />
              <Label Text="{Binding Url}"
                                   HorizontalOptions="StartAndExpand" />
              <Label Text="{Binding MandantId}"
                                   HorizontalOptions="StartAndExpand" />
              <Label Text="{Binding UpdateAt}"
                                   HorizontalOptions="StartAndExpand" />
            </StackLayout>
          </ViewCell.View>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</ContentPage>

如果这有帮助,这是我生成数据库表的模型。

class DbItem
{
    [PrimaryKey]
    public Guid Id { get; set; }
    public Guid MandantId { get; set; }
    public string Url { get; set; }
    public DateTime UpdateAt { get; set; }
}

它只显示 Url 和 UpdateAt。这是为什么?

最佳答案

我修好了。

就像 Egor Gromadskiy 的建议一样,我实现了一个 EmptyConverter ( Binding errors in Xamarin Forms )。 Xamarin 似乎无法自动将 Guid 转换为字符串。

只需在方法 Convert() 中将 value 更改为 value.ToString() 即可。

关于c# - XAML 绑定(bind)到标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39489352/

相关文章:

c# - 是否可以在 C# 中从 native C++ 类创建变量?

c# - 如何在 C# 中从字符串创建实例?

mvvm - 如何在mvvm中绑定(bind)多个同步融合xamarin表单数据网格?

android - 在网格中添加 OxyPlot View

listview - Xamarin ListView System.MissingMethodException : Method 'TextView.SetTextAppearance' not found

c# - 我可以获得有关 C# 8 皇后程序的帮助吗? (在棋盘上获得皇后类并移动)

c# - OleDB 连接字符串到 MySQL 连接字符串

c# - 是否可以将 xaml.cs 连接到 VS2017 csproj 格式的相应 xaml?

wpf - 防止 WPF 中的 Menu 控件的菜单关闭

c# - 如何将 WPF CheckBox 绑定(bind)到条件?