如何在 XAML 中将控件的内容绑定到 VIEW 类型的属性 选项 1选项 2

问题描述

我想为我的应用制作一个通用设置页面我有一个不同类型的设置列表,每一个对应如下界面:

public interface ISettings
{
    string SectionLabel { get; }

    View SectionView  { get; }
}

每种类型的设置代表应用程序设置的一个给定方面或部分。目标是让每种类型的设置定义自己的外观和调整自己值的方式。

我有一个包含所有设置列表的设置页面: public List settingsSections { get => App.AppSettings.SettingsSections; }

在 XAML 设置页面中,我想像这样显示它们:

<StackLayout BindableLayout.ItemsSource="{Binding settingsSections,Source={x:Reference this}}" Padding="0" BackgroundColor="White">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <xct:Expander>
                <xct:Expander.Header>
                    <StackLayout Orientation="Horizontal">
                        <Image Source="{Helpers:ImageResource Images.filledDownArrow.png}" HeightRequest="{Binding Source={x:Reference heightRef},Path=Height,Converter={Helpers:IntMultiplier Multiplier=0.99}}"
                            HorizontalOptions="Start"
                            VerticalOptions="Center">
                            <Image.Triggers>
                                <DataTrigger targettype="Image"
                                            Binding="{Binding Source={RelativeSource AncestorType={x:Type xct:Expander}},Path=IsExpanded}"
                                            Value="True">
                                    <Setter Property="Source"
                                        Value="{Helpers:ImageResource Images.filledRightArrow.png}" />
                                </DataTrigger>
                            </Image.Triggers>
                        </Image>
                        <Label x:Name="heightRef" Text="{Binding SectionLabel}"
                            FontAttributes="Bold"
                            FontSize="Medium" />
                    </StackLayout>
                </xct:Expander.Header>
                            
                <xct:Expander.Content="{Binding SectionView}"/>

            </xct:Expander>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

当然,这条线是行不通的:

<xct:Expander.Content="{Binding View}"/>

我怎样才能实现我的目标?有人能帮我吗 ?我想我不能使用自定义控件,因为我应该根据所引用的设置的实际类型为 stacklayout 的每一行/部分更改控件的类型。

解决方法

选项 1

此时您不必指定 xct:Expander.Content,无论您放在哪里,都会隐式设置 Content 属性。

替换

<xct:Expander.Content="{Binding SectionView}"/>

<ContentView Content="{Binding SectionView}"/>

选项 2

以这种方式指定:

<DataTemplate>
            <xct:Expander Content="{Binding SectionView}">
                <xct:Expander.Header>
...

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...