C# Xamarin表单在CollectionView中设置特定值

C# Xamarin表单在CollectionView中设置特定值,c#,xamarin,xamarin.forms,xamarin.android,xamarin.ios,C#,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,我有个问题。我创建了一个CollectionView,其中的内容是一个包含图像的列表。myImage类如下所示: public class myImage { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public string Type { get; set; } [XmlIgnore] publi

我有个问题。我创建了一个CollectionView,其中的内容是一个包含图像的列表。myImage类如下所示:

public class myImage
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Type { get; set; }
    [XmlIgnore]
    public ImageSource imageSource { get; set; }
}
public class Format
{
    public int Id { get; set; }
    public string Size{ get; set; }
    public float Price { get; set; }
    public string Type { get; set; }
}
现在在CollectionView中,我还有一个带有大小列表的选择器。该类如下所示:

public class myImage
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Type { get; set; }
    [XmlIgnore]
    public ImageSource imageSource { get; set; }
}
public class Format
{
    public int Id { get; set; }
    public string Size{ get; set; }
    public float Price { get; set; }
    public string Type { get; set; }
}
我想要的是,当myImage.Type有一个特定的值时,例如“type1”,选择器中的唯一选项将是大小,其中
Format.Type=“type1”
。对于其余部分,除
Format.Type=“type1”
中的格式外,整个列表都可以显示在选择器中

所以我想要一个带有图像的CollectionView,图像旁边有一个选择器。如果myImage.Type=“type1”,则选择器将填充Type=“type1”中的格式。图像的其余部分获取Format.Type=“Normal”所在的格式

这是我的xaml:

<DataTemplate>
    <Grid HeightRequest="100" VerticalOptions="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="10"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="3"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="3"/>
        </Grid.ColumnDefinitions>

        <ff:CachedImage  Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" Aspect="AspectFill" Source="{Binding imageSource}" />

        <Picker Grid.Row="0" Grid.RowSpan="2" Grid.Column="3" Title="Formaat" HorizontalOptions="FillAndExpand"
                ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type vm:VM_FotoLijst}}, Path=formaatList}"
                ItemDisplayBinding="{Binding Size}" PropertyChanged="FormaatPicker_PropertyChanged"/>
    </Grid>
</DataTemplate>


我该怎么做呢?

我会根据您的类型值使用两种不同的项目模板。在这些模板中,您可以将选择器的itemsource指向单个源

自定义将允许您根据项目的属性切换项目的DataTemplate。以下自定义DataTemplateSelector代码应该适用于您的用例。我还添加了一些XAML,概述了如何定义要在两者之间切换的两个数据模板。最后是一个如何在CollectionView上设置此数据模板选择器的示例

代码

public class ImageTemplateSelector : DataTemplateSelector
{
    public DataTemplate NormalTemplate { get; set; }
    public DataTemplate SpecialTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        if (item is null) return NormalTemplate;

        if (item is myImage image)
        {
            if (image.Type == "type1")
                return SpecialTemplate;
            else
                return NormalTemplate;
        }
        else
            throw new ArgumentException(nameof(ImageTemplateSelector));
    }
}
XAML

<ResourceDictionary>
    <DataTemplate x:Key="normalImageTemplate">
        <!--all the image related controls-->
        <Picker ItemsSource="{Binding NormalPickerSource}"/>
    </DataTemplate>

    <DataTemplate x:Key="specialImageTemplate">
        <!--all the image related controls-->
        <Picker ItemsSource="{Binding SpecialPickerSource}"/>
    </DataTemplate>

    <local:ImageTemplateSelector x:Key="imageTemplateSelector"
        NormalTemplate="{StaticResource normalImageTemplate}"
        SpecialTemplate="{StaticResource specialImageTemplate}" />
</ResourceDictionary>

<CollectionView ItemSource="{Binding YourItems}"
                ItemTemplate="{StaticResource imageTemplateSelector}">
</CollectionView>

我会根据您类型的值使用两种不同的项目模板。在这些模板中,您可以将选择器的itemsource指向单个源

自定义将允许您根据项目的属性切换项目的DataTemplate。以下自定义DataTemplateSelector代码应该适用于您的用例。我还添加了一些XAML,概述了如何定义要在两者之间切换的两个数据模板。最后是一个如何在CollectionView上设置此数据模板选择器的示例

代码

public class ImageTemplateSelector : DataTemplateSelector
{
    public DataTemplate NormalTemplate { get; set; }
    public DataTemplate SpecialTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        if (item is null) return NormalTemplate;

        if (item is myImage image)
        {
            if (image.Type == "type1")
                return SpecialTemplate;
            else
                return NormalTemplate;
        }
        else
            throw new ArgumentException(nameof(ImageTemplateSelector));
    }
}
XAML

<ResourceDictionary>
    <DataTemplate x:Key="normalImageTemplate">
        <!--all the image related controls-->
        <Picker ItemsSource="{Binding NormalPickerSource}"/>
    </DataTemplate>

    <DataTemplate x:Key="specialImageTemplate">
        <!--all the image related controls-->
        <Picker ItemsSource="{Binding SpecialPickerSource}"/>
    </DataTemplate>

    <local:ImageTemplateSelector x:Key="imageTemplateSelector"
        NormalTemplate="{StaticResource normalImageTemplate}"
        SpecialTemplate="{StaticResource specialImageTemplate}" />
</ResourceDictionary>

<CollectionView ItemSource="{Binding YourItems}"
                ItemTemplate="{StaticResource imageTemplateSelector}">
</CollectionView>


我不确定我是否理解。因此,您希望根据类型是否具有特定值来更改选择器中显示的数据。是的,我不确定我是否理解这一点。因此,您希望根据类型是否具有特定值来更改选择器中显示的数据。是的,这就是我想要的