Hello,
Welcome to Microsoft Q&A.
I have used the code provided by you and supplemented part of the code for testing. You could check the following code to see if it can solve your exception:
XAML
<Page.Resources>
<DataTemplate x:Key="myCellTemplate" x:DataType="local:Case">
<TextBlock Text="{Binding Name}"/>
<!-- Just for testing if the binding works-->
</DataTemplate>
</Page.Resources>
<Grid>
<controls:DataGrid x:Name="mainGrid"
ItemsSource="{x:Bind ViewModel.SingleCase.CaseTasks}">
<controls:DataGrid.Columns>
<controls:DataGridTextColumn Header="Tasks" Binding="{Binding Name}" FontWeight="SemiBold" />
</controls:DataGrid.Columns>
<controls:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
<Grid>
<controls:DataGrid
x:Name="rowDatagrid"
ItemsSource="{Binding CaseTasksDetails}">
<controls:DataGrid.Columns>
<controls:DataGridTemplateColumn Width="120" Header="Test" CellTemplate="{StaticResource myCellTemplate}"/>
</controls:DataGrid.Columns>
</controls:DataGrid>
</Grid>
</StackPanel>
</DataTemplate>
</controls:DataGrid.RowDetailsTemplate>
</controls:DataGrid>
</Grid>
Page class
public sealed partial class CaseDetailPage: Page
{
public AffaireDetailViewModel ViewModel { get; } = new AffaireDetailViewModel();
public CaseDetailPage ()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if(e.Parameter is long caseID)
{
//
}
}
}
Page ViewModel
public class AffaireDetailViewModel
{
private Case _case;
public Case SingleCase
{
get { return _case; }
set {_case =value; }
}
public AffaireDetailViewModel()
{
_case = new Case();
_case.Name = "Level1";
}
//The InitializeAsync method is replaced with the supplemented code
//public async Task InitializeAsync(long caseID)
// {
// var data = await SampleCaseDataService.GetContentGridDataAsync();
// SingleCase = data.First(i => i.caseID == caseID);
// }
}
Model classed
public class Case
{
public string Name { get; set; }
public ObservableCollection<CaseTask> CaseTasks { get; set; }
public Case()
{
CaseTasks = new ObservableCollection<CaseTask>();
CaseTasks.Add(new CaseTask() { Name = "Level2_1" });
CaseTasks.Add(new CaseTask() { Name = "Level2_2" });
CaseTasks.Add(new CaseTask() { Name = "Level2_3" });
}
}
public class CaseTask
{
public string Name { get; set; }
public ObservableCollection<CaseTaskDetail> CaseTasksDetails { get; set; }
public CaseTask()
{
CaseTasksDetails = new ObservableCollection<CaseTaskDetail>();
CaseTasksDetails.Add(new CaseTaskDetail() { Name = "Level3_1" });
CaseTasksDetails.Add(new CaseTaskDetail() { Name = "Level3_2" });
CaseTasksDetails.Add(new CaseTaskDetail() { Name = "Level3_3" });
}
}
public class CaseTaskDetail
{
public string Name { get; set; }
}
Update:
You could save the value of TestValue
which you want to access inside some level of the DataGrid
as a static value of MainPage
, and add DataGrid.LoadingRowDetails event handler to change the value of TestValue
based on the selected row in the first level of DataGrid
. And show the value by a data binding with a Converter, like this:
MainPage.xaml.cs
public static string testString = "";
……
private void dataGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
int index = dataGrid.SelectedIndex;
testString = (index+2).ToString()+"th row: "+ ViewModel.SingleCase.TestValue2;
}
Converter class
public class DateFormatter : IValueConverter
{
// This converts the DateTime object to the string to display.
public object Convert(object value, Type targetType,
object parameter, string language)
{
return MainPage.testString;
}
// No need to implement converting back on a one-way binding
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
throw new NotImplementedException();
}
}
MainPage.xaml
<Page.Resources>
<local:DateFormatter x:Key="MyConverter"/>
……
</Page.Resources>
//Use the Converter in a column inside the second level of DataGrid
<controls:DataGridTextColumn Width="120" Header="Test" Binding="{Binding Converter={StaticResource MyConverter}}"/>
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.