【问题标题】:.NET MAUI One of the ObservableCollection property changes does not fire DataTrigger and do not make any changes of UI.NET MAUI ObservableCollection 属性更改之一不会触发 DataTrigger 并且不会对 UI 进行任何更改
【发布时间】:2023-01-14 21:40:25
【问题描述】:

当在ColectionieView 中按下按钮时,我正在尝试更改按钮颜色,但数据触发器未触发,并且不应用任何 UI 更改。

我的 XAML:

 <CollectionView 
        ItemsSource="{Binding Categories}" 
        VerticalScrollBarVisibility="Never" 
        HorizontalScrollBarVisibility="Never" 
        VerticalOptions="Center">
        <CollectionView.ItemsLayout>
            <LinearItemsLayout Orientation="Horizontal" ItemSpacing="3" />
        </CollectionView.ItemsLayout>
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="model:Category">
                <Button Padding="10, 5" 
                        Margin="10, 15, 0, 10" 
                        BorderColor="Transparent" Background="{Binding Source={StaticResource Gray300}, ConverterParameter=0.4, Converter={StaticResource ColorToRGBAColor}}" 
                        VerticalOptions="Center"
                        Text="{Binding Name}"
                        Command="{Binding Source={x:Reference TaskListPage},Path=BindingContext.CategorySelectedCommand}"
                        CommandParameter="{Binding .}">
                    <Button.Triggers>
                        <DataTrigger TargetType="Button" Binding="{Binding IsActive}" Value="True">
                            <Setter Property="Background" Value="Red" />
                        </DataTrigger>
                    </Button.Triggers>
                </Button>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

视图模型:

public partial class TaskListViewModel : ObservableObject
{
    [ObservableProperty]
    private ObservableCollection<Category> categories;

    
    public IRelayCommand<Category> CategorySelectedCommand { get; set; }


    public TaskListViewModel()
    {
        categories = new ObservableCollection<Category>();

        categories.Add(new Category { Name = "Work", IsActive = true });
        categories.Add(new Category { Name = "Personal", IsActive = false });
        categories.Add(new Category { Name = "Wishlist", IsActive = false });

        CategorySelectedCommand = new RelayCommand<Category>(CategorySelected);
    }

    private void CategorySelected(Category category)
    {
        Categories.All(c => { c.IsActive = false; return true; });
        var categoryToActivate = Categories.SingleOrDefault(x => x == category);

        if (categoryToActivate is null)
        {
            return;
        }

        categoryToActivate.IsActive = true;

        OnPropertyChanged(nameof(Categories));
    }
}

我还尝试重新填充 ObservableCollection 并且这有帮助,但我确信可以有更清洁的解决方案。请给我建议我需要改变什么。

【问题讨论】:

  • 添加对 Category 及其成员 IsActive 的声明的质疑。您可能还没有让IsActive setter 执行OnPropertyChanged,因此不会告知绑定按钮值已更改。 (请记住始终显示所有相关代码。在这种情况下,绑定属性及其类的声明是相关的。)
  • @ToolmakerSteve 属性IsActiveCategory 模型的一部分。但是一般的模型不应该能够通知它们的属性被更改?或者我在这一点上错了?
  • 我不知道你所说的“一般”是什么意思。要解决您的问题,您需要通知。所以Category应该继承自ObservableObjectIsActive 应该是 ObservableProperty

标签: .net .net-maui


【解决方案1】:

请确保 Category 是一个 ObservableObject,例如:

public class Category : ObservableObject
{
    private string name;
    private bool isActive;
    public string Name
    {
        get => name;
        set => SetProperty(ref name, value);
    }
    public bool IsActive
    {
        get => isActive;
        set => SetProperty(ref isActive, value);
    }
}

更多信息,可以查看official document

【讨论】:

    猜你喜欢
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 2021-01-21
    相关资源
    最近更新 更多