【发布时间】: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