gpt4 book ai didi

xaml - Xamarin 表单 : How to reference to the parent binding

转载 作者:行者123 更新时间:2023-12-02 01:23:04 24 4
gpt4 key购买 nike

<ViewCell> 
<ViewCell.View>
<Label Text="{Binding ABC}"></Label>
</ViewCell.View>
</ViewCell>

假设此视单元位于 ListView 内部。如果内容页面与 View 模型绑定(bind),我如何获取对内容页面绑定(bind)的引用。目前,“ABC”正在引用列表中对象的属性,但我想从内容页的绑定(bind)上下文中获取值。

<ffimageloading:CachedImage.GestureRecognizers>
<TapGestureRecognizer BindingContext="{x:Reference page}" Command="{Binding OnSignInCommand}" CommandParameter="{Binding Model}" />
</ffimageloading:CachedImage.GestureRecognizers>

最佳答案

@qubuss 在下面给出了正确的答案,但我想提供更多背景信息并展示一个示例以使其更加清晰:

让我们考虑以下页面:

<ContentPage  
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="firstPage" -->this reference parent context
x:Class="Your_Class_Name">
<ListView x:Name="ListSource"
ItemsSource="{Binding ListSource}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
// this come from item source
<Label Text="{Binding ABC}"></Label>
<Button Command="{Binding BindingContext.CommandFromParent
, Source={x:Reference firstPage} }" />
</Grid>

</ViewCell>
/DataTemplate>
</ListView.ItemTemplate>
</ListView>


</ContentPage>

你的 View 模型应该看起来像这样

 public class ViewModelName 
{
private List<YourDataType> _listSource = new List<YourDataType>();


public List<YourDataType> ListSource
{
get => _listSource;
set
{
_listSource = value;
RaisePropertyChanged();
}
}

public ICommand CommandFromParent => new Command(HandleYourActionHere);

}
}

说明当我们编写 BindingContext.CommandFromParent 时,BindingContext 表示 firstPage(x:Name="firstPage") 的 BindingContext,其类型为 ViewModelName

关于xaml - Xamarin 表单 : How to reference to the parent binding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48315295/

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