【发布时间】:2021-05-06 09:45:45
【问题描述】:
我正在尝试将IsVisible 属性绑定到DataTemplate 内的StackLayout,但我无法使其工作。
我尝试过this post,但在x:Reference 中它不允许我将x:Name 放在CollectionView 中。
<CollectionView x:Name="myCollectionView"
HorizontalOptions="FillAndExpand"
ItemsSource="{Binding myBookList}">
<CollectionView.ItemTemplate>
<DataTemplate x:Name="dataTemplate">
<ContentView>
<StackLayout HorizontalOptions="EndAndExpand"
IsVisible="{Binding StackIsVisible}"
Orientation="Horizontal">
<Grid>
<ImageButton Margin="0,0,25,0"
HorizontalOptions="EndAndExpand"
Source="img1"
WidthRequest="25" />
<ImageButton Source="img2"
WidthRequest="25"
HorizontalOptions="End" />
</Grid>
<ImageButton x:Name="myImageButton"
HorizontalOptions="EndAndExpand"
Source="img3"
WidthRequest="25" />
</StackLayout>
</ContentView>
</DataTemplate>
</CollectionView.ItemTemplate>
ViewModel 和视图已正确连接,因为我有其他绑定工作,但这个对我不起作用。
private bool _StackIsVisible;
public bool StackIsVisible
{
get => _StackIsVisible;
set
{
_StackIsVisible = value;
OnPropertyChanged();
}
}
public ViewModel(){
StackIsVisible=false;
}
【问题讨论】:
-
您可以使用stackoverflow.com/a/65991607 中的引用,无需指定名称,只需输入
Binding source={x:Reference myCollectionView}
标签: xaml xamarin.forms data-binding datatemplate