gpt4 book ai didi

xamarin - 在 xamarin Forms 中绑定(bind) listView 的 Itemsource 外部的数据

转载 作者:行者123 更新时间:2023-12-02 10:10:59 25 4
gpt4 key购买 nike

您好,我正在开发 Xamarin.Forms,我在 ListView 中遇到数据绑定(bind)问题。 model 中有一个字符串值,model 中有一个列表,列表是 ListView 的 ItemsSource,字符串值应该显示在每个单元格行中。

XMAL View

<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>

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();
}

我知道我可以将 ConstantName 添加到 PagingButtons 类,但我确实有 30-40 类的这种情况,这样做会很乏味并且不会有效这样做的方法。

最佳答案

我假设有一个 ContentPage在你身边ListView ,但这可以是任何页面(甚至元素)。

为该页面命名(如果还没有),如下所示:<ContentPage x:Name="MyAwesomePage" ...>

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

这样,您就可以将绑定(bind)引用到页面的 BindingContext ,有效地改变范围。

关于xamarin - 在 xamarin Forms 中绑定(bind) listView 的 Itemsource 外部的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54303384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com