Я использую collectionView и хочу использовать собственный стиль для выбранного элемента. Я уже пытаюсь связать стиль со свойством, но все еще не могу изменить только стиль выбранного элемента.
Кто-нибудь уже делал это? Спасибо
Вот код:
ListTypePOI = new CollectionView()
{
SelectionMode = SelectionMode.Single,
ItemsLayout = new GridItemsLayout(ItemsLayoutOrientation.Horizontal),
HeightRequest = 60,
BackgroundColor = (Color)Application.Current.Resources["LightBackgroundColor"],
VerticalScrollBarVisibility = ScrollBarVisibility.Never,
VerticalOptions = LayoutOptions.Start,
};
ListTypePOI.SetBinding(SelectableItemsView.SelectedItemProperty, "SelectedTypePOI", BindingMode.TwoWay);
ListTypePOI.SetBinding(ItemsView.ItemsSourceProperty, "TypesPOI");
ListTypePOI.ItemTemplate = new DataTemplate(() =>
{
Frame frame = new Frame()
{
WidthRequest = 90,
Margin = new Thickness(5, 5, 5, 5),
VerticalOptions = LayoutOptions.Start,
Padding = 5,
};
var tap = new TapGestureRecognizer();
tap.Tapped += Tap_Tapped;
frame.GestureRecognizers.Add(tap);
if (compteur < TypesPOI.Count)
{
StackLayout stackLayout = new StackLayout() { Orientation = StackOrientation.Horizontal, Padding = new Thickness(5, 5, 5, 5) };
Label lbl = new Label
{
Style = (Xamarin.Forms.Style)Application.Current.Resources["MainLabelStyle"],
LineBreakMode = LineBreakMode.TailTruncation,
FontSize = this.FontSizeXXSmall,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
};
lbl.SetBinding(Label.TextProperty, "Libelle");
var type = TypesPOI.ElementAt(compteur);
Image image = new Image
{
Aspect = Aspect.AspectFit,
VerticalOptions = LayoutOptions.Center
};
image.Source = ImageSource.FromStream(() => new MemoryStream(type.Icone));
stackLayout.Children.Add(image);
stackLayout.Children.Add(lbl);
frame.Content = stackLayout;
compteur++;
}
Binding b = new Binding("StyleFrameClicked", BindingMode.OneWay, source: this);
frame.SetBinding(Frame.StyleProperty,b);
return frame;
});
ListTypePOI.Style = (Xamarin.Forms.Style)Application.Current.Resources["SelectItemTypePOI"];
StyleFrameClicked = (Xamarin.Forms.Style)Application.Current.Resources["ListViewCellFrameStyle"];
FrameTypePoi.Content = ListTypePOI;
private void Tap_Tapped(object sender, EventArgs e)
{
StyleFrameClicked = (Xamarin.Forms.Style)Application.Current.Resources["ListViewCellFrameStyle"];
Frame frame = (Frame)sender;
frame.Style = (Xamarin.Forms.Style)Application.Current.Resources["ListViewCellClickedFrameStyle"];
}
Я использовал GestureRecognizer для обнаружения щелчка моего кадра