【问题标题】:Access property in ListView that is not coming from ItemsSource访问 ListView 中不是来自 ItemsSource 的属性
【发布时间】:2021-07-03 17:57:48
【问题描述】:

我的ListView 绑定到MyTexts 列表。尽管如此,我需要将TextCell's Text 绑定到不是来自Texts (StandAloneProperty) 的属性。我该怎么做?

<ListView
            SelectedItem="{Binding SelectedText, Mode=TwoWay}"
            ItemsSource="{Binding MyTexts}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell
                        Text="{Binding StandAloneProperty, StringFormat='Value: {0}'}"
                        TextColor="{Binding Color}"
                    />                    
                </DataTemplate>
            </ListView.ItemTemplate>
</ListView>

【问题讨论】:

  • StandAloneProperty 在哪里定义?它是在与MyTexts 相同的基础虚拟机上还是在其他地方?你读过Relative Bindings吗?
  • @Jason 它位于同一个虚拟机中
  • 参见上述链接文档中的“绑定到祖先”示例

标签: xaml xamarin xamarin.forms data-binding


【解决方案1】:

将您的绑定Source 设置为当前页面,并从您的ViewModel BindingContext 访问StandAloneProperty

<ContentPage x:Name="pageRef"
...

<ListView
            SelectedItem="{Binding SelectedText, Mode=TwoWay}"
            ItemsSource="{Binding MyTexts}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell
                        Text="{Binding Source={x:Reference pageRef} Path=BindingContext.StandAloneProperty, StringFormat='Value: {0}'}"
                        TextColor="{Binding Color}"
                    />                    
                </DataTemplate>
            </ListView.ItemTemplate>
</ListView>

【讨论】:

    猜你喜欢
    • 2012-01-08
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多