【问题标题】:Change Style name for some view inside ViewCell upon Listview TappedItem, Xamarin Forms在 Listview TappedItem、Xamarin Forms 上更改 ViewCell 中某些视图的样式名称
【发布时间】:2022-05-02 17:03:56
【问题描述】:

我有 Xamarin Forms 应用程序和 Listview,看起来像这样:

<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;
            }
        }
    }

现在的问题是:

选择单元格后,是否可以将 PCV: PancakeView 的样式更改为不同的样式名称?

我正在使用 Xamarin Forms 版本 4.3.0 顺便说一句。

我已经能够更改整个单元格的颜色,但我不确定如何更改样式。

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    根据你的描述,我猜你想在 ListView ItemTapped 事件中改变 PancakeView 的样式?

    如果是,我会做一个样本,你可以看看。我用Label控件代替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");
            }
        }
    
    }
    

    ViewModelBase 是实现 INotifyPropertyChanged 的​​类

     public class ViewModelBase : INotifyPropertyChanged
    {
          
        public event PropertyChangedEventHandler PropertyChanged;
    
        
        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    当您在 ListView 中点击一项时,样式会发生变化。

    【讨论】:

    • 谢谢,DataTrigger 解决了。欣赏它。
    猜你喜欢
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多