如何给SfChipGroup的ChipBackground绑定属性?

问题描述 投票:0回答:1

我使用 SfChipGroup 并将 ViewModel 中的 PageButtonList 绑定为 ItemSource。

private ObservableCollection<PageModel> _pageButtonList = new ObservableCollection<PageModel>();

public ObservableCollection<PageModel> PageButtonList
{
    get { return _pageButtonList; }
    set
    {
        _pageButtonList = value;
        OnPropertyChanged("PageButtonList");
    }
}

对于背景颜色,我将使用 PageButtonList 对象 IsCurrentPage 中的布尔属性。 我使用的是 BoolToColorConverter

<toolkit:BoolToObjectConverter x:Key="BoolToColorConverter" 
                       TrueObject="{x:Static Colors.Red}"
                       FalseObject="{x:Static Colors.White}"/>

然后像这样使用它:

<syncfusion:SfChipGroup ItemsSource="{Binding PageButtonList}"
                        DisplayMemberPath="Title"
                        ChipBackground="{Binding IsCurrentPage, Converter={StaticResource BoolToColorConverter}}"
                        ChipTextColor="Black"/>

列表对象类PageModel看起来像这样:

public class PageModel
{
    public int Id { get; set; }

    public string Title => (Id + 1).ToString();

    public bool IsCurrentPage { get; set; }

    public PageModel(int id)
    {
        Id = id;
        IsCurrentPage = true;
    }
}

问题是我收到此错误:

绑定:在“MyApp.ViewModels.MyPageViewModel”上找不到属性“IsCurrentPage”。

属性 IsCurrentPage 属于对象 PageButtonList,该对象是 MyPageViewModel 的一部分。错误消息是正确的,但是我如何告诉SfChipGroup DataType 是PageModel

maui syncfusion
1个回答
0
投票

你想要做的事情是不可能的。您需要为 SfChipGroup.ItemTemplate 提供一个

DataTemplate
,但这也意味着您必须在该模板中自行完全配置
SfChip

<syncfusion:SfChipGroup ItemsSource="{Binding PageButtonList}">
    <syncfusion:SfChipGroup.ItemTemplate>
        <DataTemplate x:DataType="models:PageModel">
            <syncfusion:SfChip
                Text="{Binding Title}"
                TextColor="Black"
                Background="{Binding IsCurrentPage, Converter={StaticResource BoolToColorConverter}}" />
        </DataTemplate>
    </syncfusion:SfChipGroup.ItemTemplate>
</syncfusion:SfChipGroup>
© www.soinside.com 2019 - 2024. All rights reserved.