将属性绑定到Listview DataTemplate中的元素 [英] Bind properties to elements inside a Listview DataTemplate

查看:64
本文介绍了将属性绑定到Listview DataTemplate中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Xamarin形式的 DataTemplate 中使用数据绑定.我可以将其与 ListView 一起使用(即绑定 RowHeight ),但是一旦在 DataTemplate 中,将其设置为我的属性ViewModel没有影响.

I can't get databinding to work within a DataTemplate in Xamarin forms. I can get it to work with the ListView (i.e. binding the RowHeight), but once in the DataTemplate, setting things to a property of my ViewModel has no affect.

在下面的示例中,如果将 ColumnDefinition.Width 设置为属性,则将其完全忽略,但是将其设置为硬值可以很好地工作.我已经放入标签以查看属性值是什么,并且在 Datatemplate 内时它变成空白,并且我重新检查了该属性是否正确,因为如果我从标签中取出标签, Listview ,它显示正确的值.

In the below example, if I set the ColumnDefinition.Width to a property, it is completely ignored, but setting it to a hard value works fine. I have put in a label to see what the property value is and it turns out blank when inside the Datatemplate, and I have re-checked that the property is correct because if I take the label out of the Listview, it displays the correct value.

有什么想法吗?

更新:

我的班级是"ParentPage";具有2个属性:患者"和设置".患者具有可观察到的患者列表"集合,我将如何绑定到设置.字体大小".在标签中显示如下.我尝试了所有我能想到的组合:

My Class is "ParentPage" which has 2 properties: "Patients" and "Settings". Patients has an observable collection of "PatientList", How would I bind to the "Settings.Fontsize" shown below in the label. I have tried every combination I can think of:

<ListView   x:Name="listView" ItemsSource="{Binding ParentPage.Patients.PatientsList}" RowHeight="{Binding ParentPage.Settings.RowHeight}"  >
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <Grid HorizontalOptions="FillAndExpand">
          <Grid.RowDefinitions>
            <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <Image  Grid.Row="0" Grid.Column="0"
             Source="{Binding Picture}" />

          <Label  TextColor="Red" Text="{Binding ParentPage.Settings.FontSize}"   
       HorizontalOptions="Center" />

        </Grid>     
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

推荐答案

BindingContext,换句话说,用于Listview的"ItemsSource"适用于整个listview,包括DataTemplate.因此,如果您想绑定某些东西,则它的属性应该在BindingContext中,在您的情况下为Patient.因此,Pasient类应包含此属性.但是,有一个窍门.要绑定的东西不在列表视图绑定上下文中,则应命名列表视图,例如x:Name ="YourListView",然后在用于绑定的数据模板中编写以下代码:

BindingContext, in other words "ItemsSource" for listview, is appliying to whole listview, include DataTemplate. So, if you wanna bind something, then it property should be in BindingContext, in your case it is Patients. So, Pasient class should include this property. But, there is a trick. Where you want bind something, that is not in listview binding context, then you should name your listview, like x:Name = "YourListView" and then in your datatemplate for your binding write this:

Property="{Binding Source={x:Reference Name = "YourListView"},
          Path=BindingContext.YourNameOfPropertyInViewModel}"

通过此操作,您的属性将使用listview元素级别的绑定上下文,在您的情况下为ViewModel.

With this, your property will use binding context of your listview element level, that is ViewModel in your case.

这篇关于将属性绑定到Listview DataTemplate中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆