Xamarin.forms 在Listview TappedItem、Xamarin窗体上更改ViewCell内某些视图的样式名称
我有Xamarin Forms应用程序和Listview,其外观如下:Xamarin.forms 在Listview TappedItem、Xamarin窗体上更改ViewCell内某些视图的样式名称,xamarin.forms,Xamarin.forms,我有Xamarin Forms应用程序和Listview,其外观如下: <ListView x:Name="CalendarList" VerticalOptions="FillAndExpand" VerticalScrollBarVisibility="Never" RowHeight="100" Grid.Row="0" SeparatorVisibility="None" ItemTapped="CalendarList_OnItemSelected"
<ListView x:Name="CalendarList" VerticalOptions="FillAndExpand" VerticalScrollBarVisibility="Never" RowHeight="100"
Grid.Row="0" SeparatorVisibility="None" ItemTapped="CalendarList_OnItemSelected" BackgroundColor="Transparent" >
<ListView.ItemTemplate>
<DataTemplate>
<local:MyCell>
<pcv:PancakeView **x:Name="YearsContainer"** Margin = "0,10,0,10" Style="{StaticResource cell_years}" IsClippedToBounds="true" >
<StackLayout HorizontalOptions = "StartAndExpand" Orientation="Horizontal">
<Label Style = "{DynamicResource bold_label}" Text="{Binding Year}" VerticalOptions="Center" />
</StackLayout>
</pcv:PancakeView>
</local:MyCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
private async void CalendarList_OnItemSelected(object sender, EventArgs e)
{
await Task.Delay(350);
var selectedItem = ((ListView)sender).SelectedItem;
var item = ((YearsList)selectedItem);
((ListView)sender).SelectedItem = null;
vm.ViewDetailCommand.Execute(item); // goto details page
}
public class MyCell: ViewCell
{
protected async override void OnTapped()
{
base.OnTapped();
await Task.Run(async () => await AnimationHelper.AnimateClick(this.View));
}
}
//IOS Renderer
[assembly: ExportRenderer(typeof(MyCell), typeof(MyCellRenderer))]
namespace CountDown.iOS.Renderers
{
public class MyCellRenderer : ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
if (cell != null)
{
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
}
return cell;
}
}
}
已选择专用异步无效日历列表(对象发送方,事件参数e)
{
等待任务。延迟(350);
var selectedItem=((列表视图)发送方)。selectedItem;
变量项=((年份列表)选择编辑项);
((列表视图)发件人)。选择EdItem=null;
vm.ViewDetailCommand.Execute(item);//转到详细信息页
}
公共类MyCell:ViewCell
{
受保护的异步重写void OnTapped()
{
base.OnTapped();
wait Task.Run(异步()=>wait AnimationHelper.AnimateClick(this.View));
}
}
//IOS渲染器
[程序集:ExportRenderer(typeof(MyCell),typeof(MyCellRenderer))]
命名空间倒计时.iOS.Renderers
{
公共类MyCellRenderer:ViewCellRenderer
{
公用覆盖UITableViewCell GetCell(单元格项目、UITableViewCell可重用单元格、UITableView电视)
{
var cell=base.GetCell(item、reusableCell、tv);
如果(单元格!=null)
{
cell.SelectionStyle=UITableViewCellSelectionStyle.None;
}
返回单元;
}
}
}
现在的问题是:
选择单元格后,是否可以将PCV:PancakeView的样式更改为不同的样式名称
我使用的是XamarinFormsVer4.3.0BTW
我已经能够更改整个单元格的颜色,但我不确定如何更改样式。根据您的描述,我猜您想更改ListView事件中的PancakeView样式 如果是,我做一个样品,你可以看看。我使用标签控件而不是PancakeView,这是相同的
<ContentPage.Resources>
<Style x:Key="LabelStyle" TargetType="Label">
<Setter Property="TextColor" Value="Color.Black" />
<Setter Property="FontAttributes" Value="None" />
</Style>
<Style x:Key="LabelChangedStyle" TargetType="Label">
<Setter Property="TextColor" Value="Color.Red" />
<Setter Property="FontAttributes" Value="Bold" />
</Style>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<ListView ItemTapped="ListView_ItemTapped" ItemsSource="{Binding model3s}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Style="{Binding LabelStyle}" Text="{Binding str}">
<Label.Triggers>
<DataTrigger
Binding="{Binding istap}"
TargetType="Label"
Value="true">
<Setter Property="Style" Value="{StaticResource LabelChangedStyle}" />
</DataTrigger>
</Label.Triggers>
</Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
public partial class Page12 : ContentPage
{
public ObservableCollection<model3> model3s { get; set; }
public model3 model;
public Page12 ()
{
InitializeComponent ();
model3s = new ObservableCollection<model3>()
{
new model3(){str="this is test!",istap=false },
new model3(){str="this is test!",istap=false},
new model3(){str="this is test!",istap=false},
new model3(){str="this is test!",istap=false},
new model3(){str="this is test!",istap=false}
};
this.BindingContext = this;
}
private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
if(model!=null)
{
model.istap = false;
}
model3 m = e.Item as model3;
m.istap = true;
model = m;
}
}
public class model3:ViewModelBase
{
public string str { get; set; }
private bool _istap;
public bool istap
{
get { return _istap; }
set
{
_istap = value;
RaisePropertyChanged("istap");
}
}
}
在ListView中点击一个项目时,样式将更改
如果我的答复对您有帮助,请记住将我的答复标记为“答复”,谢谢。谢谢,我已经解决了。谢谢。
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}