【问题标题】:Object binding does not work对象绑定不起作用
【发布时间】:2018-05-03 18:23:49
【问题描述】:

我正在使用 XF 列表视图,并且想绑定整个对象,然后使用转换器根据某些条件格式化数据,但是对象绑定在 XF 中不再起作用。测试在 XF 2.5.0.91635 和 android 支持库 26.1.0.1 上。首先,当不使用项目模板时,列表视图会正确呈现,尽管内容显示的是默认对象字符串:

<ListView x:Name="lv"></ListView>

但是当使用项目模板时,没有显示任何内容,尽管项目的数量确实添加到了列表视图中,并且项目选择事件被正确触发。此外,调试输出以下消息:

        <ListView x:Name="lv">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding}"></Label>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'
[0:] Binding: Models.DataItem can not be converted to type 'System.String'

当我将转换器添加到绑定表达式时,转换函数始终在其参数中接收 null。

添加绑定代码:

List<DataItem> list = new List<DataItem>();
        for(int i = 0; i < 10; i++)
        {
            list.Add(new DataItem
            {
                N = "L " + i.ToString(),
                C = i + 1,
            });
        }
        lv.ItemsSource = list;

【问题讨论】:

  • 不完全确定发生了什么,但我是否正确理解你这样定义它&lt;Label Text="{Binding}"&gt;&lt;/Label&gt;?通常你会像&lt;Label Text="{Binding PropertyOnModel}"&gt;&lt;/Label&gt; 这样定义它,其中PropertyOnModel 是数据绑定模型上的一个属性,或者至少&lt;Label Text="{Binding .}"&gt;&lt;/Label&gt; 绑定到整个模型对象。这能解决问题吗?
  • 是的,绑定到属性有效,但我故意想绑定到整个对象并将其传递给转换器,但它不再起作用,{Binding .}{Binding Path=.} 的问题相同.

标签: c# xaml data-binding xamarin.forms xamarin.android


【解决方案1】:

当我将转换器添加到绑定表达式时,转换函数总是在其参数中接收 null。

如果你想绑定到整个对象,你需要像这样修改DataItem

public class DataItem
{
    public String N { get; set; }
    public int C { get; set; }

    //use an Item property to reference itself
    public DataItem Item { get { return this; } }
}

然后你可以在你的ListView 中使用它和转换器:

<ListView  x:Name="lv" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label Text="{Binding Item,Converter={StaticResource YourConverter}}" ></Label>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
 </ListView>

【讨论】:

  • 太棒了,从来没有想过这种方式,谢谢!你是如何得到这个想法的,你有这个解决方法的任何来源或文档吗?
  • 抱歉没有关于这个的文档,只是缺乏经验。
【解决方案2】:

正确的绑定方式是“将集合绑定到 ListView”并为列表提供ItemSource

例如:

private ObservableCollection<Models.DataItem> _dataList;
public ObservableCollection<Models.DataItem> DataList
{
    get { return _dataList; }
    set
    {
        _dataList = value;
        RaisePropertyChanged(() => DataList); // you can use your own property notification mechanism here.
    }
}

然后在为您的 ViewModel 设置绑定上下文之后:

<ListView ItemsSource="{Binding DataList}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label Text="{Binding . }" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

或者如果你想显示来自DataItem的属性:

public class Model.DataItem
{
    public string Property1 {get; set; } 
    public string Property2 {get; set; }
    public string Property3 {get; set; }
}

使用

<ViewCell>
    <Label Text="{Binding Property1 }" />
    <Label Text="{Binding Property2 }" />
    <Label Text="{Binding Property3 }" />
</ViewCell>

希望对你有帮助

【讨论】:

  • 感谢发帖,但不是我要的,我知道如何绑定到集合和属性绑定。这里的问题是 xamarin 运行时以某种方式无法处理对象绑定,即使使用转换器 - 它总是接收 null。
  • 您能否发布您的“实际”代码,包括视图模型和转换器代码。因为你的问题没有正确的意义。
  • 我刚刚添加了绑定代码。如果您实际测试过这个问题,那么这个问题就很有意义了。问题就在没有涉及转换器的情况下,我只是使用转换器进一步测试以确认存在问题。
  • 按照我的回答,当您在后面的代码中设置 ItemSource 时,您可以显示 DataItem 属性(N,C)。您仍然没有添加转换器代码(如果我知道您如何转换对象,会更容易找到问题)。你可以实现 ToString() 来获得正确的字符串转换。
  • 就是这个问题,添加ToString到对象后绑定还是失败。您可以使用存根转换器并打印出 value 参数,您会注意到它是 null。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 2013-12-28
  • 2020-11-26
  • 1970-01-01
  • 2017-03-15
  • 2018-09-16
  • 1970-01-01
相关资源
最近更新 更多