【问题标题】:How to set a empty path for the Binding in Xamarin?如何为 Xamarin 中的绑定设置空路径?
【发布时间】:2020-10-08 08:32:24
【问题描述】:

我有一个绑定,我不需要路径。如果我在 xaml 中设置 ItemTemplate 效果很好。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="TestBindingPath.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <ListView x:Name="listview1" ItemsSource="{Binding Students}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label Text="{Binding}" />
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

但是如果我在代码中设置 ItemTemplate 会引发异常

listview1.ItemTemplate = new DataTemplate(() => {
    Label label = new Label();
    label.SetBinding(Label.TextProperty, new Binding());
    return (new ViewCell() {
        View = label
    });
});

那么如何在代码中为 Binding 设置一个空路径呢?

【问题讨论】:

  • new Binding(""); 或 new Binding(".");
  • 这个问题解决了吗?如果是这样,请发布您的答案。它将帮助其他有类似主题的人,谢谢。还有一篇关于绑定的有用文章。docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…
  • 您好,new Binding("") 不起作用,但 new Binding(".") 有效。

标签: xamarin binding


【解决方案1】:

这是绑定方法结构。

 public Binding(string path, BindingMode mode = BindingMode.Default, IValueConverter converter = null, object converterParameter = null, string stringFormat = null, object source = null);

试着这样做。

    listview1.ItemTemplate = new DataTemplate(() => {
    Label label = new Label();
    label.SetBinding(Label.TextProperty, new Binding(source:Students));
    return (new ViewCell() {
        View = label
    });
});

Students 属性必须在代码 bihind cs 中

【讨论】:

    猜你喜欢
    • 2020-10-26
    • 2019-06-16
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 2018-08-17
    相关资源
    最近更新 更多