c# - 从代码访问 DataTemplate 的元素
问题描述
我想找到一种ButtonImage
从 c# 访问元素的方法。中包含的所有DataTemplate
内容都不能从 c# 调用。
<CarouselView
x:Name="FrasiView"
RelativeLayout.WidthConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Width,
Factor=1}"
RelativeLayout.YConstraint="{ConstraintExpression
Type=Constant,
Constant=45}"
PeekAreaInsets="18"
HeightRequest="370">
<CarouselView.ItemsLayout>
<LinearItemsLayout Orientation="Horizontal" SnapPointsType="MandatorySingle" SnapPointsAlignment="Center"/>
</CarouselView.ItemsLayout>
<CarouselView.ItemTemplate>
<DataTemplate>
<RelativeLayout>
<RelativeLayout x:Name="FrontView">
<RelativeLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="FlipToBack_Tapped"/>
</RelativeLayout.GestureRecognizers>
<ImageButton
x:Name="CuoreVuoto"
RelativeLayout.YConstraint="{ConstraintExpression
Type=Constant,
Constant=22}"
RelativeLayout.XConstraint="{ConstraintExpression
Type=Constant,
Constant=275}"
Source="IconHeart"
WidthRequest="24"
Clicked="CuoreVuoto_Clicked"
BackgroundColor="Transparent"/>
<ImageButton
x:Name="CuorePieno"
RelativeLayout.YConstraint="{ConstraintExpression
Type=Constant,
Constant=22}"
RelativeLayout.XConstraint="{ConstraintExpression
Type=Constant,
Constant=275}"
Source="IconHeart"
WidthRequest="24"
Clicked="CuorePieno_Clicked"
IsVisible="False"
BackgroundColor="Transparent"/>
</RelativeLayout>
</RelativeLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
在c#中我想回忆一下CuorePieno
元素我该怎么办?
解决方案
我希望当用户按下 ImageButton 时,IsVisible 属性会发生变化
我同意 Jason 的回复,您可以使用 Binding 来更改 ImageButton IsVisible 属性,我做了一个示例,您可以看看:
<RelativeLayout>
<CarouselView
x:Name="FrasiView"
HeightRequest="370"
ItemsSource="{Binding lists}"
PeekAreaInsets="18"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=1}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,
Constant=45}">
<CarouselView.ItemsLayout>
<LinearItemsLayout
Orientation="Horizontal"
SnapPointsAlignment="Center"
SnapPointsType="MandatorySingle" />
</CarouselView.ItemsLayout>
<CarouselView.ItemTemplate>
<DataTemplate>
<RelativeLayout x:Name="FrontView">
<Label Text="{Binding str}" />
<ImageButton
x:Name="CuoreVuoto"
BackgroundColor="Transparent"
Clicked="CuoreVuoto_Clicked"
IsVisible="{Binding Vuoto}"
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,
Constant=275}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,
Constant=22}"
Source="c1.png"
WidthRequest="24" />
<ImageButton
x:Name="CuorePieno"
BackgroundColor="Transparent"
Clicked="CuorePieno_Clicked"
IsVisible="{Binding Pieno}"
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,
Constant=275}"
RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,
Constant=22}"
Source="check.png"
WidthRequest="24" />
</RelativeLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
</RelativeLayout>
代码隐藏
public partial class Page1 : ContentPage
{
public ObservableCollection<carouselmodel> lists { get; set; }
public Page1()
{
InitializeComponent();
lists = new ObservableCollection<carouselmodel>()
{
new carouselmodel(){str="test 1",Vuoto=true,Pieno=false},
new carouselmodel(){str="test 2",Vuoto=true,Pieno=false},
new carouselmodel(){str="test 3",Vuoto=true,Pieno=false},
new carouselmodel(){str="test 4",Vuoto=true,Pieno=false},
new carouselmodel(){str="test 5",Vuoto=true,Pieno=false},
new carouselmodel(){str="test 6",Vuoto=true,Pieno=false}
};
this.BindingContext = this;
}
private void CuoreVuoto_Clicked(object sender, EventArgs e)
{
var button = sender as ImageButton;
var model = button.BindingContext as carouselmodel;
if(model.Vuoto==true)
{
model.Vuoto = false;
model.Pieno = true;
}
}
private void CuorePieno_Clicked(object sender, EventArgs e)
{
var button = sender as ImageButton;
var model = button.BindingContext as carouselmodel;
if (model.Pieno == true)
{
model.Pieno = false;
model.Vuoto = true;
}
}
}
public class carouselmodel : ViewModelBase
{
public string str { get; set; }
private bool _Vuoto;
public bool Vuoto
{
get { return _Vuoto; }
set
{
_Vuoto = value;
RaisePropertyChanged("Vuoto");
}
}
private bool _Pieno;
public bool Pieno
{
get
{ return _Pieno; }
set
{
_Pieno = value;
RaisePropertyChanged("Pieno");
}
}
}
ViewModelBase 类继承INotifyPropertyChanged接口,对nofity 数据进行更改。
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
推荐阅读
- python - 用 Flask @auth.login_required 装饰器装饰的端点的单元测试
- android - 活动!!将 android studio 更新到 4.1 时显示红色下划线
- verilog - 如何在 verliog 中进行更准确的计算 ln(1+x)
- javascript - 在列表项中添加条件
- java - 从外部类调用时 Java Swing 标签不更新
- r - R - 如何从 emp 和 supervisor 的 2 列添加报告级别
- javascript - jquery-ajax 将html元素值从一个页面传递到另一个页面
- javascript - 为什么 v2 未定义但具有数组中最后一个元素的值?
- c++ - 打印出一个对象数组 C++
- css - CSS中的子菜单背景颜色