0

I can not seem to figure out the problem making my second screen in WPF. If I only make 1 screen my datacontext works fine. If I make a second it doesn't seem to work anymore (looks like it doesn't work anymore on both screens).

This is the code from my Main window (if I only have this window, the code works perfectly fine):

<Grid Background="#FF63A29A">
        <Image x:Name="TrackImage" HorizontalAlignment="Left" Height="984" VerticalAlignment="Top" Width="1750" Margin="80,42,0,0"/>
        <Label FontSize="16" FontWeight="Bold" Height="1002" VerticalAlignment="Top" Margin="821,0,815,0" Content="{Binding TrackName, Mode=OneWay}">
            <Label.DataContext>
                <local:DataContext/>
            </Label.DataContext>
        </Label>
        <DockPanel LastChildFill="False">
            <Menu DockPanel.Dock="Top" Height="20" Width="54.84">
                <MenuItem x:Name="Menu" HeaderStringFormat="Menu" Header="Menu" FontSize="16" HorizontalAlignment="Center" Click="Menu_Click">
                    <MenuItem x:Name="Current_Race" Click="MenuItem_Open_CurrentRaceScreen" Header="Current Race"/>
                    <MenuItem x:Name="Current_Competition" Click="MenuItem_Open_CurrentCompetitionScreen" Header="Current Competition"/>
                    <MenuItem x:Name="CloseApplication" Header="Close Application" Click="MenuItem_Exit_Click"/>
                </MenuItem>
            </Menu>

        </DockPanel>
    </Grid>

The thing that I am trying to do in the following window is to show a list of the RaceStats (see DataContext class code) which I'm also probably doing wrong... This is the code of the window that won't work:

    <Grid>
        <Grid.DataContext>
            <local:DataContext/>
        </Grid.DataContext>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <DockPanel Grid.Column="0" LastChildFill="False"/>
        <ListView x:Name="RaceStatsLV" d:ItemsSource="{Binding RaceStats}">
            <ListView.View>
                <GridView>
                    <GridViewColumn/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

I get the error: Object Reference not set to an instance of an object. The error is about the datacontext.

The DataContext class has the following code:

   public class DataContext : INotifyPropertyChanged
   {
      public event PropertyChangedEventHandler? PropertyChanged;
      public string TrackName => Data.CurrentRace.Track.Name;
      public List<string> RaceStats => Data.CurrentRace.RaceStats.Select(x => $"{x.Key.Name} » Points: {x.Value}").ToList();

      public List<string> RoundCount => Data.CurrentRace.Participants.Select(x => $"{x.Name} round {x.Rounds}/{Data.CurrentRace.Track.Rounds}").ToList();

      public DataContext()
      {
         Data.CurrentRace.DriversChanged += DriversChangedEventMethod;
         Data.CurrentRace.RaceChanged += RaceChangedEventMethod;
      }

      public void DriversChangedEventMethod(object? s, DriversChangedEventArgs e)
      {
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(""));
      }

      public void RaceChangedEventMethod(Race previousRace, Race nextRace)
      {
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(""));
      }
   }

I hope someone sees what I'm doing wrong and thank you for even putting in the effort!

Irish
  • 9
  • 1
  • 1
    Where is the instance of the DataContext? The same instance has to be used throughout your code. You need to pass the instance to code that is not working or make it public. – jdweng Oct 23 '22 at 11:50
  • Be aware that `` in XAML creates an instance of the DataContext class, so you have at least two. If two or more UI elements (like your Windows) are supposed to operate on the same data, they should share a single view model instance. – Clemens Oct 24 '22 at 08:21

0 Answers0