【问题标题】:How to bind listview item binding to Viewmodel of the page?如何将listview项绑定绑定到页面的Viewmodel?
【发布时间】:2019-11-09 16:47:26
【问题描述】:

我想将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 的绑定上下文?
  • @Jason 是的。我会根据一些事件不断改变它
  • @haldo 抱歉,在这里错过了该声明。 this.BindingContext = ViewModel;
  • 如果我没记错的话,该属性需要是公共的而不是私有的。您是否尝试过将private MyViewModel ViewModel 设为公开而不是私有?

标签: c# listview xamarin xamarin.forms binding


【解决方案1】:

我解决了如下

<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 使用这种方式调用引用的绑定上下文的路径

感谢所有建议!非常感谢这个社区的及时响应!

【讨论】:

    【解决方案2】:

    迟到的答案,但可能会帮助某人。 如果您在后面的代码中编写布局。您可以以这种方式将上下文绑定到源视图模型上下文。我正在将按钮绑定到视图模型中的命令。

    btn.SetBinding(Button.CommandProperty, new Binding("BindingContext.YourCommand", source: YourListView));
    btn.SetBinding(Button.CommandParameterProperty, ".");
    

    【讨论】:

      【解决方案3】:

      两步..

      1. 使用x:Name="viewName"(绑定到 ViewModel 的视图)为您的父视图提供参考名称

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

      这行得通。

      【讨论】:

      • 有人愿意解释为什么这会被否决吗?
      猜你喜欢
      • 2019-12-22
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 2021-05-21
      • 1970-01-01
      • 2023-03-18
      • 2012-11-15
      相关资源
      最近更新 更多