Reputation: 13
I have two label fields TabName and DisplayDate and a Collection Tasks bound to a ViewModel, and the value of the Label fields are initially set but changes don't appear. Hovering over the label binding values displays "No DataContext found for Binding 'fieldname'". The Collection is bound correctly and shows changes.
Here is my XAML code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:DailyTasks_MVVM.ViewModels"
xmlns:DataType="vm:DailyTasksViewModel"
xmlns:m="clr-namespace:DailyTasks.CoreBusiness;assembly=DailyTasks.CoreBusiness"
x:Class="DailyTasks_MVVM.Views.TaskPage"
Title="">
<Grid
Padding="5"
RowDefinitions="30, 25, *, 10, 45, 10"
ColumnDefinitions="*">
<Label Grid.Row="0" Grid.Column="0" Padding="0,-4"
FontSize="Medium" FontAttributes="Bold"
HorizontalOptions="Center"
Text="{Binding TabName}"></Label>
<HorizontalStackLayout Grid.Row="1" Grid.Column="0">
<Grid Padding="9,-10" RowDefinitions="25" ColumnDefinitions="315">
<Label Text="{Binding DisplayDate}"
Grid.Row="0" Grid.Column="1"
HorizontalTextAlignment="Center" FontSize="Medium"></Label>
</Grid>
</HorizontalStackLayout>
<CollectionView
x:Name="TaskCollection"
ItemsSource="{Binding Tasks}"
Grid.Row="2"
Grid.Column="0">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="m:cTask">
<StackLayout>
<Frame Margin="0,1,0,1">
<StackLayout Orientation="Vertical" Margin="-10" Spacing="0">
<HorizontalStackLayout>
<CheckBox VerticalOptions="Start"></CheckBox>
<VerticalStackLayout>
<Label Margin="2,9" FontSize="Medium" Text="{Binding TaskName}" />
<Label Margin="2,0" Text="{Binding HasLingered}" />
</VerticalStackLayout>
</HorizontalStackLayout>
</StackLayout>
</Frame>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button
Grid.Row="4"
Grid.Column="0"
MaximumWidthRequest="50"
MaximumHeightRequest="60"
Scale="1.1"
FontSize="Large"
Command="{Binding GotoAddTaskCommand}"
Text="+">
</Button>
</Grid>
</ContentPage>
Here is the properties of the view model DailyTasksViewModel.
namespace DailyTasks_MVVM.ViewModels
{
public partial class DailyTasksViewModel : ObservableObject
{
private const string MainTab = "Main";
private readonly IViewSpecificDateUseCase viewSpecificDateUseCase;
[ObservableProperty]
private DateTime currentDate;
[ObservableProperty]
private cTask task;
[ObservableProperty]
private Guid? currentTabID;
[ObservableProperty]
private string tabName;
[ObservableProperty]
public cTaskInfo taskInfo;
public string DisplayDate
{
get { return CurrentDate.ToString("d"); }
}
public ObservableCollection<cTask> Tasks { get; set; }
public DailyTasksViewModel(IViewSpecificDateUseCase viewSpecificDateUseCase)
{
this.viewSpecificDateUseCase = viewSpecificDateUseCase;
this.Tasks = new ObservableCollection<cTask>();
this.TabName = "Main";
this.CurrentDate = DateTime.Now.Date;
}
}
}
I looked at examples of setting the Source and Path in the Binding but couldn't figure out what values I needed to use.
ex: Text="{Binding Source={x:Reference DailyTasksViewModel}, Path=TabName}"
I expect the DataContext not found message to go away and the new values display when the property values are changed.
Upvotes: 0
Views: 654
Reputation: 13
Thanks for looking into this. I found the problem I was having. I had a property called DisplayDate that was returning the formatted version CurrentDate.
public string DisplayDate
{
get { return CurrentDate.ToString("d"); }
}
When the value of CurrentDate was updated, apparently no change to DisplayDate was recognized so the value wasn't updated on the page.
To solve this I changed the binding to use the CurrentDate and added the formatting with the binding.
Text="{Binding CurrentDate, StringFormat='{0:MM/dd/yyyy}'}"
Even though the message "No DataContext found for Binding 'TabName'" shows up in the editor, the binding still works.
Hovering over the TabName or DisplayDate field showed the message
Upvotes: 1