【问题标题】:Xamarin Forms binding collection of strings c#Xamarin Forms绑定字符串集合c#
【发布时间】:2016-07-19 11:02:07
【问题描述】:

我必须将字符串集合绑定到包含具有这些字符串的元素的列表视图,但我不知道该怎么做:

name.SetBinding(Label.TextProperty, ??? );

我知道在 xaml 中我可以做到这一点

<Label Text="{Binding }"/>

但是代码隐藏中的等价物是什么? 我需要 customcell,因为 listitem 还有其他元素

视图模型:

private BindableCollection<string> _wifiNetworks;
    public BindableCollection<string> WifiNetworks
    {
        get { return _wifiNetworks; }
        set
        {
            if (value != _wifiNetworks)
            {
                _wifiNetworks = value;
                NotifyOfPropertyChange(() => WifiNetworks);
            }
        }
    }

在视图中:

   var wifiList = new ListView
        {
             RowHeight = 50
        };
        wifiList.SetBinding(ListView.ItemsSourceProperty, new Binding("WifiNetworks"));
        wifiList.ItemTemplate = new DataTemplate(typeof(CustomCell));

CustomCell.cs:

public CustomCell()
    {                          

        var name = new Label
        {
            TextColor= (Color)Application.Current.Resources["ForegroundColor"],
            FontSize = 17               
        };

        name.SetBinding(Label.TextProperty, ??? );

    }

【问题讨论】:

标签: c# xamarin xamarin.forms


【解决方案1】:

“。”绑定路径将属性直接绑定到 BindingContext

name.SetBinding(Label.TextProperty, "." );

【讨论】:

    猜你喜欢
    • 2015-11-05
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多