C# UWP中ComboBox的CompositeCollection的真正替代方案?

C# UWP中ComboBox的CompositeCollection的真正替代方案?,c#,uwp,combobox,uwp-xaml,C#,Uwp,Combobox,Uwp Xaml,请注意,还有一个类似的问题,它不能回答缺少合成收集的问题,而公认的答案是可怕的: 在WPF中,我可以做到: <ComboBox> <ComboBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection="{Binding MyData}" /> <!-- add custom XAML t

请注意,还有一个类似的问题,它不能回答缺少合成收集的问题,而公认的答案是可怕的:

在WPF中,我可以做到:

<ComboBox>
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{Binding MyData}" />
            <!-- add custom XAML to combobox here -->
            <ComboBoxItem><Button Command="{Binding Magic}">Clear</Button></ComboBoxItem>
            <my:CustomControl/>
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>

清楚的
在UWP中,我找到的唯一方法是定义DataTemplateSelector,并将人工项添加到我的ItemsSource中:

<ComboBox>
    <ComboBox.ItemTemplateSelector>
        <local:CustomTemplateSelector>
            <local:CustomTemplateSelector.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"></TextBlock>
                </DataTemplate>
            </local:CustomTemplateSelector.ItemTemplate>
            <local:CustomTemplateSelector.ButtonTemplate>
                <DataTemplate>
                    <Button Content="{Binding}" Command="{Binding Magic}"></Button>
                </DataTemplate>
            </local:CustomTemplateSelector.ButtonTemplate>
        </local:CustomTemplateSelector>
    </ComboBox.ItemTemplateSelector>
</ComboBox>

公共类CustomTemplateSelector:DataTemplateSelector
{
公共数据模板ItemTemplate{get;set;}
公共数据模板ButtonTemplate{get;set;}
受保护的覆盖数据模板SelectTemplateCore(对象项,DependencyObject容器)
{
返回GetDataTemplate(项目);
}
受保护的覆盖数据模板SelectTemplateCore(对象项)
{
返回GetDataTemplate(项目);
}
私有数据模板GetDataTemplate(对象项)
{
if(项目为IButtonMarkerInterface)
{
返回按钮模板;
}
返回项目模板;
}
}
//为组合框标记按钮的人工项
公共接口IButtonMarkerInterface
{
}
//此外,还需要包装可观察集合的集合viewSource
公共类CollectionWrapper:INotifyCollectionChanged
{
public CollectionWrapper(INotifyCollectionChanged wrappedCollection,IEnumerable staticItems)
{
雅达雅达雅达雅达

您可以一目了然地看出为什么WPF解决方案更好。有更好的方法吗?

要使多个集合在UWP中的组合框中显示为单个列表,就像WPF中的CompositeCollection一样,最好使用DataTemplateSelector选择将显示的模板,就像您当前所做的那样。要使多个集合显示为单个列表,请UWP中的ComboBox中只有一个列表,就像WPF中的CompositeCollection一样,最好使用DataTemplateSelector来选择要显示的模板,就像您当前所做的那样。
public class CustomTemplateSelector : DataTemplateSelector
{
    public DataTemplate ItemTemplate { get; set; }

    public DataTemplate ButtonTemplate { get; set; }


    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        return GetDataTemplate(item);
    }

    protected override DataTemplate SelectTemplateCore(object item)
    {
        return GetDataTemplate(item);
    }

    private DataTemplate GetDataTemplate(object item)
    {
        if (item is IButtonMarkerInterface)
        {
            return ButtonTemplate;
        }

        return ItemTemplate;
    }
}

// Artificial item which marks buttons for combobox
public interface IButtonMarkerInterface
{

}

// Also You need collection viewSource which wraps observable collection
public class CollectionWrapper : INotifyCollectionChanged
{
    public CollectionWrapper(INotifyCollectionChanged wrappedCollection, IEnumerable<object> staticItems)
    {
        Yadda yadda yadda