以xamarin形式绑定来自listView的Itemsource外部的数据

Roh*_*pat 3 xamarin xamarin.forms

您好,我正在使用Xamarin.Forms,但我面临的是数据绑定问题ListView。模型中有一个字符串值,模型中有一个列表,该列表是ItemsSourceListView 的列表,字符串值应显示在每个单元格行中。

XMAL视图

<ListView ItemsSource="{Binding StudentList, Mode=TwoWay}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text={Binding Name} />
                    <Label Text={Binding ConstantName} /> //// Not present in Itemsource. Should bind from Model. It does not render.
                 </Stacklayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

C#

public ObservableCollection<PagingButtons> _lstPagingButtons { get; set; }
public ObservableCollection<PagingButtons> lstPagingButtons
{
    get { return _lstPagingButtons; }
    set
    {
        _lstPagingButtons = value;
        OnPropertyChanged();
    }
}
string _ConstantName {get; set;} = "Xamarin";
public string ConstantName 
{
    get { return _ConstantName; }
    set { __ConstantName = value; OnPropertyChange(); 
}
Run Code Online (Sandbox Code Playgroud)

我知道,我可以添加ConstantNamePagingButtons类,但我确实有此方案为30-40级,这将是乏味做到这一点,不会是这样做的有效途径。

Ger*_*uis 6

我将假设ContentPage您周围有个ListView,但这可以是任何页面(甚至元素)。

给该页面起一个名称(如果尚无名称),如下所示: <ContentPage x:Name="MyAwesomePage" ...>

现在,将您更改Label为:<Label Text={Binding BindingContext.ConstantName, Source={x:Reference Name=MyAwesomePage}} />

这样,您将绑定引用到页面的BindingContext,有效地改变了范围。