我正在使用堆栈视图中的可绑定布局来创建类别的水平列表,如下图所示,因此当我单击某个类别时,我想更改文本颜色,但是在实际绑定中不会更新视图在财产变更后被解雇。
<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;
}
}
答案 0 :(得分:0)
能给我一个例子,说明如何在RaisePropertyChanged中 二传手。
尝试将您的SpicesCategory
模型更改为:
public class SpicesCategory :ExtendedBindableObject
{
public long Id { get; set; }
public bool IsSelected { get; set; }
private string _name { get; set; }
public string Name
{
get => _name;
set
{
_name = value;
RaisePropertyChanged(() => (Name));
}
}
private Color _nameColor { get; set; }
public Color NameColor
{
get => _nameColor;
set
{
_nameColor = value;
RaisePropertyChanged(() => (NameColor));
}
}
}