如何将ListView的项绑定到页面的ViewModel?

3
我希望将ListView中的项目绑定到ViewModel中的属性上,而不是绑定到ItemsSource上。但是在尝试Binding Source={x:Reference Name=ThisPage} Path=ViewModel.TimerValue时它并没有起作用,我做错了什么,无法确定问题所在。
我尝试设置:
Text="{Binding Path=TimerValue, TargetNullValue='00:00', FallbackValue='00:00', StringFormat='{0:mm\\:ss}', Source={x:Reference Name=ThisPage}}"

ViewModel实现了INotifyPropertyChanged接口,并引发了PropertyChanged事件。

页面标题 - 参考

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App3.Views.MyPage"
             x:Name="ThisPage">

<ListView x:Name="listView" ItemsSource={Binding Items}>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding Path=TimerValue, TargetNullValue='00:00', FallbackValue='00:00', StringFormat='{0:mm\\:ss}', Source={x:Reference Name=ThisPage}}" />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

后端代码

 private MyViewModel ViewModel;
 public MyPage () {
     InitializeComponent ();
     ViewModel = new MyViewModel ();
     this.BindingContext = ViewModel;
 }

你是在哪里/如何为XAML设置绑定上下文的? - haldo
@Knoop,不用了。让我试试看。 - Morse
它需要是一个公共属性。 - Jason
应该是 <Label Text="{Binding Path=ViewModel.TimerValue, TargetNullValue='00:00', FallbackValue='00:00', StringFormat='{0:mm\\:ss}', Source={x:Reference MyPage}}" BindingContext="{x:Reference MyPage.ViewModel}"/> - Morse
我遇到了一个错误,App3.Views.MyPage 中找不到属性 ViewModel 的绑定。现在尝试使用 <Label Text="{Binding TimerValue, TargetNullValue='00:00', FallbackValue='00:00', StringFormat='{0:mm\\:ss}'}" BindingContext="{Binding Source={x:Reference MyPage}, Path=BindingContext}" - Morse
显示剩余4条评论
3个回答

3
我以下面的方式解决了它。
<Label Text="{Binding TimerValue, TargetNullValue='00:00', FallbackValue='00:00', StringFormat='{0:mm\\:ss}'}"
        BindingContext="{Binding Source={x:Reference MyPage}, Path=BindingContext}">

绑定出现了问题,BindingContext必须是BindableObjectBindingContext是可绑定对象,它又引用了ViewModel对象,而Label.Text必须是可绑定对象的BindableProperty。 当我引用Text={Binding ViewModel.TimerValue时,它试图在Mypage中查找可绑定属性,但ViewModel只是一个公共属性,不是可绑定对象,BindingContext = ViewModel将其转换为可绑定对象,因此我必须使用这种方式来调用Source和Text并调用所引用绑定上下文的路径。
感谢所有建议!真的非常感谢这个社区的快速响应!

1
晚回答了,但可能对某些人有所帮助。 如果您正在编写代码中的布局,则可以以此方式将上下文绑定到源视图模型上下文。我正在将按钮绑定到视图模型内部的命令。
btn.SetBinding(Button.CommandProperty, new Binding("BindingContext.YourCommand", source: YourListView));
btn.SetBinding(Button.CommandParameterProperty, ".");

-2

两个步骤..

  1. 使用 x:Name="viewName" 为父视图(绑定到ViewModel的视图)命名引用名称

  2. 进行绑定: "{Binding BindingContext.MenuTapCommand, Source={x:Reference viewName}}"

这样就可以了。


有人可以解释一下为什么这个被踩了吗? - Abhijith C R
可能是因为它是重复的,而且没有增加价值吧?@Abhijith C R - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接