I am using C#, WPF, .NET Standard, Visual Studio. All the latest or almost latest versions.
This is my datacontext model (which is created in seperated library called ProgrammingManagerAPI):
public class MainModel
{
public List<Project> Projects { get; set; }
...
}
which have list of object of type Project defined like this (also in seperated library ProgrammingManagerAPI, in directory Models), some properties and some methods:
public class Project
{
public int Id { get; set; }
...
public TimeSpan? TotalWorkedTime(bool subtasksIncluded = true)
{
if (Id < 0)
return null;
else
return new TimeSpan(...);
}
...
}
In mainWindow I have a ListView, which I want to use to list projects with its properties.
I have lots of properties and some methods which are giving back the value depending on boolean parameter.
I read that in this case I should use ObjectDataProvider, so I tried like below:
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:API.Models="clr-namespace:ProgrammingManagerAPI.Models;assembly=ProgrammingManagerAPI"
<Window.Resources>
<ObjectDataProvider x:Key="yourStaticData"
ObjectType="{x:Type API.Models:Project}"
MethodName="TotalWorkedTime" >
<ObjectDataProvider.MethodParameters>
<s:Boolean>false</s:Boolean>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid Grid.Row="1" Grid.Column="0" Margin="10">
<ListView Margin="10" ItemsSource="{Binding Projects}" HorizontalAlignment="Center" HorizontalContentAlignment="Center">
<ListView.View>
<GridView>
<GridViewColumn HeaderContainerStyle="{StaticResource ListViewStyle}" Header="Id" DisplayMemberBinding="{Binding Id}" />
<GridViewColumn HeaderContainerStyle="{StaticResource ListViewStyle}" Header="TotalWorkedTime" DisplayMemberBinding="{Binding Path=., Source={StaticResource yourStaticData}}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
The call to the function TotalWorkedTime is fired, because breakpoint is hit. But is hit once, while I have created 4 object for test. Moreover it is hit like static function, not for every instance of the object like other properties. In immediate window I am trying to see what are other properties and those are nulls. While the column for Id is hit all the properties are available for each instance of Project. Moreover I have observed that it is hit before Id property getter is called.
I have tried many versions like without Path, in Binding and so many others ways.
Anyone can point me my mistake?