作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<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/
我是一名优秀的程序员,十分优秀!