我希望将
我尝试设置:
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;
}
<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}"/>
- MorseApp3.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