Я создаю горизонтальный список категорий, используя привязываемый макет в виде стека, как на изображении, показанном ниже, поэтому при нажатии на одну из моих категорий я хочу изменить цвет текста, но в реальном Привязка не обновляет вид после изменения свойства было уволено.
<StackLayout x:Name="CategoryStack" BindableLayout.ItemsSource="{Binding CategoryListItems,Mode=TwoWay}"
Orientation="Horizontal" Padding="5,3,0,3" BackgroundColor="Transparent">
<BindableLayout.ItemTemplate>
<DataTemplate >
<custom:PancakeView BackgroundColor="White" Grid.Row="0" Grid.Column="0" IsClippedToBounds="true" Padding="4" HeightRequest="47" CornerRadius="5">
<Grid>
<Label HorizontalTextAlignment="Center" Margin="0" VerticalOptions="Center" FontSize="Small" Text="{Binding Name}" TextColor="{Binding NameColor,Mode=TwoWay}">
</Label>
</Grid>
<custom:PancakeView.GestureRecognizers>
<TapGestureRecognizer CommandParameter="{Binding .}" Command="{Binding Path=BindingContext.CategoryTappedCmd,Source={x:Reference CategoryStack}}" NumberOfTapsRequired="1" />
</custom:PancakeView.GestureRecognizers>
</custom:PancakeView>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
ниже мой код ViewModel
public class ProductsListViewModel : ViewModelBase
{
private ObservableCollection<SpicesCategory> _CategoryListItems = new ObservableCollection<SpicesCategory>();
public ObservableCollection<SpicesCategory> CategoryListItems
{
get => _CategoryListItems;
set
{
_CategoryListItems = value;
RaisePropertyChanged(() => (CategoryListItems));
}
}
public ICommand CategoryTappedCmd => new Command(CategoryTapped);
public async void CategoryTapped(object obj)
{
SpicesCategory SelectedspicesCategory = obj as SpicesCategory;
foreach (var item in CategoryListItems)
{
if(item == SelectedspicesCategory)
{
item.IsSelected = true;
item.NameColor = Color.Red;
}
else
{
item.IsSelected = false;
item.NameColor = Color.Black;
}
}
}
}
и ниже моя модель SpicesCategory
public class SpicesCategory
{
public long Id { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
public Color NameColor { get; set; }
}
ниже мой ViewModelBase, наследующий ExtendedBindableObject
public class ViewModelBase : ExtendedBindableObject
{
public ViewModelBase(INavigationService navigationService)
{
}
private bool _isBusy;
public event PropertyChangedEventHandler PropertyChanged;
public bool IsBusy
{
get => _isBusy;
set
{
_isBusy = value;
RaisePropertyChanged(() =>(IsBusy));
}
}
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public virtual Task InitializeAsync(object data)
{
return Task.FromResult(false);
}
}
и ниже - мой ExtendedBindableObject, наследующий BindableObject
public abstract class ExtendedBindableObject : BindableObject
{
public void RaisePropertyChanged<T>(Expression<Func<T>> property)
{
var name = GetMemberInfo(property).Name;
OnPropertyChanged(name);
}
private MemberInfo GetMemberInfo(Expression expression)
{
MemberExpression operand;
LambdaExpression lambdaExpression = (LambdaExpression)expression;
if (lambdaExpression.Body as UnaryExpression != null)
{
UnaryExpression body = (UnaryExpression)lambdaExpression.Body;
operand = (MemberExpression)body.Operand;
}
else
{
operand = (MemberExpression)lambdaExpression.Body;
}
return operand.Member;
}
}