简体   繁体   English

如何在位于 Bindable StackLayouts 内的 ItemTemplateSelector 中引用父 DataTemplates 参数?

[英]How do you reference a parent DataTemplates parameter inside an ItemTemplateSelector located inside of a Bindable StackLayouts?

This sounds like a mouthful but I have a BindableLayout.ItemTemplateSelector that is located inside of a DataTemplate .这听起来像一口,但我有一个BindableLayout.ItemTemplateSelector位于DataTemplate The problem is I can not pass a property of the DataTemplate 's ItemSource for some reason.问题是由于某种原因我无法传递DataTemplateItemSource的属性。

In section 'Data Template' , of 'The Code' below, I am trying to access properties inside of ItemSource="{Binding CarouselItems}" from within <controls:ThoughtEntryDataTemplateSelector> .在下面“代码”的“数据模板”部分中,我试图从<controls:ThoughtEntryDataTemplateSelector>访问ItemSource="{Binding CarouselItems}"内的<controls:ThoughtEntryDataTemplateSelector> However, I can't access anything inside of there except for StaticResource 's.但是,除了StaticResource之外,我无法访问其中的任何内容。

I'd like to pass a property from CarouselItems into it我想将CarouselItems的属性传递给它

The Code编码

Data Template数据模板

                    <cards:CarouselView
                        x:Name="ThoughtCarouselViewer"
                        IndicatorView="indicatorView"
                        IsCyclical="False"
                        ItemsSource="{Binding CarouselItems}">
                        <cards:CarouselView.ItemTemplate>
                            <DataTemplate>
                                <StackLayout>
                                    <Label
                                        x:Name="Title"
                                        FontAttributes="Bold"
                                        Text="{Binding Header}"
                                        TextColor="{StaticResource TextColor}" />
                                    <rv:MaterialFrame
                                        Padding="5"
                                        LightThemeBackgroundColor="#F1F1F1"
                                        Style="{StaticResource CardView}">
                                        <StackLayout BindableLayout.ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type local:ThoughtRecordViewModel}}, Path=ThoughtEntryContainers}" Orientation="Vertical">
                                            <BindableLayout.ItemTemplateSelector>
                                                <controls:ThoughtEntryDataTemplateSelector
                                                    ChallengingThought="{StaticResource ChallengingThought}"
                                                    Distortions="{StaticResource Distortions}"
                                                    NegativeThought="{StaticResource NegativeThought}"
                                                    ThoughtType="{Binding ThoughtEntryType}" /> <============================== What I am trying to access
                                            </BindableLayout.ItemTemplateSelector>
                                        </StackLayout>
                                    </rv:MaterialFrame>
                                </StackLayout>
                            </DataTemplate>
                        </cards:CarouselView.ItemTemplate>
                    </cards:CarouselView>

DataTemplateSelector数据模板选择器

    public class ThoughtEntryDataTemplateSelector : DataTemplateSelector
    {
        public DataTemplate NegativeThought { get; set; }
        public DataTemplate Distortions { get; set; }
        public DataTemplate ChallengingThought { get; set; }
        public ThoughtEntryType ThoughtType { get; set; }

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            DataTemplate dataTemplate;

            if(ThoughtType == ThoughtEntryType.NegativeThought)
            {
                dataTemplate = NegativeThought;
            } else if(ThoughtType == ThoughtEntryType.Distortion)
            {
                dataTemplate = Distortions;
            } else
            {
                dataTemplate = ChallengingThought;
            }

            return dataTemplate;
        }
    }

Carousel Items旋转木马项目

    public class ThoughtEntryCarouselItems
    {
        private string header;
        public string Header
        {
            get => header;
            set => header = value;
        }

        private ThoughtEntryType thoughtEntryType;
        public ThoughtEntryType ThoughtEntryType
        {
            get => thoughtEntryType;
            set => thoughtEntryType = value;
        }
    }

According to Creating a Xamarin.Forms DataTemplateSelector , I suggest you can choose a DataTemplate for CarouselView, not for StackLayout, then you can get current item ThoughtEntryType in OnSelectTemplate method, please see my code.根据Creating a Xamarin.Forms DataTemplateSelector ,我建议您可以为CarouselView 选择DataTemplate,而不是为StackLayout,然后您可以在OnSelectTemplate方法中获取当前项目ThoughtEntryType,请参阅我的代码。

 <ContentPage.Resources>
    <DataTemplate x:Key="ChallengingThought">
        <StackLayout >
            <Label FontAttributes="Bold" Text="{Binding Header}" />
            <Frame CornerRadius="30" BorderColor="Black" BackgroundColor="AliceBlue">
                <StackLayout BindableLayout.ItemsSource="{Binding EntryContainers}" Orientation="Vertical">
                    <Label Text="{Binding .}" />
                </StackLayout>
            </Frame>
        </StackLayout>
    </DataTemplate>
    <DataTemplate x:Key="NegativeThought" >
        <StackLayout>
            <Label FontAttributes="Bold" Text="{Binding Header}" />
            <Frame  CornerRadius="30" BorderColor="Black" BackgroundColor="DarkGray">
                <StackLayout BindableLayout.ItemsSource="{Binding EntryContainers}" Orientation="Vertical">
                    <Label Text="{Binding .}" />
                </StackLayout>
            </Frame>
        </StackLayout>
    </DataTemplate>
    <DataTemplate x:Key="Distortions">
        <StackLayout >
            <Label FontAttributes="Bold" Text="{Binding Header}" />
            <Frame CornerRadius="30" BorderColor="Black" BackgroundColor="YellowGreen">
                <StackLayout BindableLayout.ItemsSource="{Binding EntryContainers}" Orientation="Vertical">
                    <Label Text="{Binding .}" />
                </StackLayout>
            </Frame>
        </StackLayout>
    </DataTemplate>
    <local:ThoughtEntryDataTemplateSelector x:Key="EntryDataTemplateSelector"
            ChallengingThought="{StaticResource ChallengingThought}"
                                                Distortions="{StaticResource Distortions}"
                                                NegativeThought="{StaticResource NegativeThought}"/>
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout>
        <CarouselView
                    x:Name="ThoughtCarouselViewer"                                          
                    ItemsSource="{Binding CarouselItems}" ItemTemplate="{StaticResource EntryDataTemplateSelector}">

        </CarouselView>
    </StackLayout>
</ContentPage.Content>

 public partial class Page24 : ContentPage
{
    public Page24()
    {
        InitializeComponent();
        this.BindingContext = new ThoughtRecordViewModel();
    }
}
public class ThoughtRecordViewModel
{
    public ObservableCollection<ThoughtEntryCarouselItems> CarouselItems { get; set; }
    public ThoughtRecordViewModel()
    {
        CarouselItems = new ObservableCollection<ThoughtEntryCarouselItems>()
        {
            new ThoughtEntryCarouselItems(){Header="header 1",EntryType=ThoughtEntryType.ChallengingThought,EntryContainers=new ObservableCollection<string>(){"1","2","3"}},
            new ThoughtEntryCarouselItems(){Header="header 2",EntryType=ThoughtEntryType.Distortions,EntryContainers=new ObservableCollection<string>(){"a","b","c"}},
            new ThoughtEntryCarouselItems(){Header="header 3",EntryType=ThoughtEntryType.NegativeThought,EntryContainers=new ObservableCollection<string>(){"11","22","33"}},
            new ThoughtEntryCarouselItems(){Header="header 4",EntryType=ThoughtEntryType.ChallengingThought,EntryContainers=new ObservableCollection<string>(){"112","2222","3333"}},

        };
    }
}

public class ThoughtEntryCarouselItems
{
    private string header;
    public string Header
    {
        get => header;
        set => header = value;
    }

    private ThoughtEntryType _EntryType;
    public ThoughtEntryType EntryType
    {
        get => _EntryType;
        set => _EntryType = value;
    }

    public ObservableCollection<string> EntryContainers { get; set; }
}
public enum ThoughtEntryType
{
    NegativeThought, Distortions, ChallengingThought
}
public class ThoughtEntryDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate NegativeThought { get; set; }
    public DataTemplate Distortions { get; set; }
    public DataTemplate ChallengingThought { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        DataTemplate dataTemplate=null;
        ThoughtEntryCarouselItems carouseitem = (ThoughtEntryCarouselItems)item;

        if (carouseitem.EntryType == ThoughtEntryType.NegativeThought)
        {
            dataTemplate = NegativeThought;
        }
        else if (carouseitem.EntryType == ThoughtEntryType.Distortions)
        {
            dataTemplate = Distortions;
        }
        else if(carouseitem.EntryType == ThoughtEntryType.ChallengingThought)
        {
            dataTemplate = ChallengingThought;
        }
        return dataTemplate;

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF在DataTemplates中改进了ItemTemplateSelector - WPF emended ItemTemplateSelector in a DataTemplates 在用户控件内设置Treeview的ItemTemplateSelector - Set the ItemTemplateSelector of a treeview inside a user control 如何动态绑定具有 ItemTemplateSelector 的 ListView 的 ItemSource,其中 ItemTemplateSelector 包含具有不同 DataTypes 的多个 DataTemplates? - How to dynamically bind the ItemSource of a ListView which has ItemTemplateSelector containing Multiple DataTemplates which have Different DataTypes? 如何实现“Xamarin.Forms DataTemplateView 中的两个可绑定属性”? - How to implement 'Xamarin.Forms Two bindable properties inside a DataTemplateView'? 如何使自定义XAML用户控件可绑定? - How do you make a custom XAML User Control bindable? 在C#中,如何从另一个内存汇编中引用类型? - In C#, how do you reference types from one in-memory assembly inside another? 如何找到位于datalist itemtemplate内的控件 - How to find a control located inside datalist itemtemplate 如何创建自动动画轮播,以便带有DataTemplates的GridView中的内容开始使用C#或XAML滚动? - How to create an automatic animated carousel so content inside a GridView with DataTemplates starts scrolling using C# or XAML? 如何获取位于文件夹内的文件的日期时间? - How to get the datetime of a file located inside a folder? 如何在函数中实例化空引用参数? - How do you instantiate a null reference parameter in a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM