I used your code to make a demo, and both of DataGrid work well.
Code for Xaml:
<Window.DataContext>
<local:viewModel></local:viewModel>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" ItemsSource="{Binding SkaterStats,NotifyOnSourceUpdated=True ,NotifyOnTargetUpdated=True}"
CanUserAddRows="False" IsReadOnly="True" FrozenColumnCount="1"
AutoGenerateColumns="False" Margin="0,0,0,20" Sorting="DataGrid_Sorting" TargetUpdated="DataGrid_TargetUpdated">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Go to Player" Click="LetsGoToPlayer"></MenuItem>
</ContextMenu>
</TextBlock.ContextMenu></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid Grid.Row="1" ItemsSource="{Binding SkaterStats,NotifyOnSourceUpdated=True ,NotifyOnTargetUpdated=True}"
CanUserAddRows="False" IsReadOnly="True" FrozenColumnCount="2" ScrollViewer.CanContentScroll="False"
AutoGenerateColumns="False" Margin="0,0,0,20"
Sorting="DataGrid_Sorting" TargetUpdated="DataGrid_TargetUpdated" LoadingRow="theLoadingRow">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button>
<Button.Content>
<TextBlock Text="{Binding Name}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Go to Player" Click="doti"></MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</Button.Content>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
The cs code is:
public class viewModel
{
public ObservableCollection<SkaterState> SkaterStats { get; set; } = new ObservableCollection<SkaterState>();
public viewModel()
{
SkaterStats = new ObservableCollection<SkaterState>();
SkaterStats.Add(new SkaterState() { ID = 1, Name = "a", State = "0" });
SkaterStats.Add(new SkaterState() { ID = 2, Name = "aa", State = "1" });
}
}
public class SkaterState
{
public SkaterState() { }
public int ID { get; set; }
public string Name { get; set; }
public string State { get; set; }
}
The result picture is:
Does my demo work for you? If it doesn't, did I miss any step or code to reproduce your error?
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.