I'm fairly new to C# and WPF. I created a small application that lets you open the explorer and chose a file, from there the program will take all the files from the folder that the chosen file was chosen from and show all the files in DataGrid. From there you can click on a cell and it will show some simple properties on the right side under "Details". But the problem comes when you've clicked on a file in the DataGrid and then want to chose a new folder to inspect. So you click on the button that opens the explorer and when you go to choose a new file it crashes and I get a System.NullReferenceException.
WPF Code:
<Window x:Class="RFAnalyzerMain.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RFAnalyzerMain"
mc:Ignorable="d"
Title="MainWindow"
Height="600" Width="750"
MinHeight="500" MinWidth="650"
FontSize="11">
<Grid>
<StackPanel>
<!-- Menu bar -->
<DockPanel>
<Menu DockPanel.Dock="Top"
Padding="2" Background="#eeefff"
BorderThickness="0.5" BorderBrush="Black">
<MenuItem Header="_Arkiv" Foreground="Black" FontSize="11">
<MenuItem Header="_Folder to Read"
Click="OpenExplorerAndShowFilesAndFolderName_Click" />
</MenuItem>
</Menu>
</DockPanel>
<!-- Tool bar -->
<DockPanel Background="#eeefff" Height="18">
<Button x:Name="openExplorer" HorizontalAlignment="Left"
Background="#eeefff" BorderThickness="0"
Margin="2 0" Click="OpenExplorerAndShowFilesAndFolderName_Click">
<Image Source="Images/open-folder.png" Margin="5 0" Width="18"/>
</Button>
</DockPanel>
<!-- Folder Read Text -->
<TextBlock Text="Folder read:"
FontWeight="Bold" FontSize="13"
Margin="10 6 0 0" />
<!-- Folder Read TextBox & More Options Button -->
<Grid Margin="0 5">
<!-- Grid Definitions -->
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="67" />
</Grid.ColumnDefinitions>
<TextBox x:Name="folderPath" Text="" IsReadOnly="True" Background="#eee" Margin="10 0 0 0" />
<Button x:Name="moreContentButton" Content="..." Grid.Column="1" Margin="0 0 10 0"
HorizontalAlignment="Right" Width="47" Click="OpenExplorerAndShowFilesAndFolderName_Click" />
</Grid>
</StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="347" />
</Grid.ColumnDefinitions>
<!-- Datagrid -->
<DataGrid x:Name="ResultFilesDataGrid" Margin="10 100 10 25" AutoGenerateColumns="False"
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
Grid.Column="0" SelectionMode="Single" SelectedCellsChanged="ResultFilesDataGrid_SelectedCellsChanged">
<DataGrid.Columns>
<DataGridTextColumn x:Name="DataGridValue" IsReadOnly="True" Binding="{Binding}" Header="Files" />
<DataGridTextColumn IsReadOnly="True" Header="Filtyp" />
</DataGrid.Columns>
</DataGrid>
<Border Grid.Column="1" Margin="0 100 10 25"
Width="337" HorizontalAlignment="Right" Background="#eee"
BorderThickness="1" BorderBrush="Gray">
<ScrollViewer Padding="3" >
<StackPanel>
<TextBlock Text="Details" FontSize="13" FontWeight="Bold" Margin="0 0 0 2"/>
<!-- Separator -->
<Separator Margin="0 5" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Filtyp:" Grid.Column="0" Grid.Row="0" Margin="0 3" />
<TextBox x:Name="fileType" Text="" Margin="0 3"
Grid.Column="1" Grid.Row="0"
IsReadOnly="True" BorderThickness="0" Background="#eee" />
<TextBlock Text="Öppnas med:" Grid.Column="0" Grid.Row="1" Margin="0 3" />
<TextBox x:Name="openWith" Text="Foton" Margin="0 3"
Grid.Column="1" Grid.Row="1"
IsReadOnly="True" BorderThickness="0" Background="#eee" />
</Grid>
<!-- Separator -->
<Separator Margin="0 5" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Plats:" Grid.Column="0" Grid.Row="0" Margin="0 5" />
<TextBox x:Name="pathToFolder" Text="" Margin="0 5"
Grid.Column="1" Grid.Row="0"
IsReadOnly="True" BorderThickness="0" Background="#eee" />
<TextBlock Text="Storlek:" Grid.Column="0" Grid.Row="1" Margin="0 5" />
<TextBox x:Name="fileSize" Text="" Margin="0 5"
Grid.Column="1" Grid.Row="1"
IsReadOnly="True" BorderThickness="0" Background="#eee" />
<TextBlock Text="Storlek på disk:" Grid.Column="0" Grid.Row="2" Margin="0 5" />
<TextBox x:Name="fileSizeOnDisk" Text="?" Margin="0 5"
Grid.Column="1" Grid.Row="2"
IsReadOnly="True" BorderThickness="0" Background="#eee" />
</Grid>
<!-- Separator -->
<Separator Margin="0 5" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Skapad:" Grid.Column="0" Grid.Row="0" Margin="0 5" />
<TextBox x:Name="fileCreated" Text="" Margin="0 5"
Grid.Column="1" Grid.Row="0"
IsReadOnly="True" BorderThickness="0" Background="#eee" />
<TextBlock Text="Ändrad:" Grid.Column="0" Grid.Row="1" Margin="0 5" />
<TextBox x:Name="fileChanged" Text="" Margin="0 5"
Grid.Column="1" Grid.Row="1"
IsReadOnly="True" BorderThickness="0" Background="#eee" />
<TextBlock Text="Använd:" Grid.Column="0" Grid.Row="2" Margin="0 5" />
<TextBox x:Name="fileUsed" Text="" Margin="0 5"
Grid.Column="1" Grid.Row="2"
IsReadOnly="True" BorderThickness="0" Background="#eee" />
</Grid>
<!-- Separator -->
<Separator Margin="0 5" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="79" />
<ColumnDefinition Width="1.5*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="1.25*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Attribut:" Grid.Column="0" Grid.Row="0" Margin="0 5" />
<CheckBox Content="Skrivskydd" VerticalContentAlignment="Center" Margin="0 5"
Grid.Column="1" Grid.Row="0" />
<CheckBox Content="Dold" VerticalContentAlignment="Center" Margin="0 5"
Grid.Column="2" Grid.Row="0" />
<Button Content="Avancerat..." Height="20"
Grid.Column="3" Grid.Row="0"/>
<TextBlock Text="Säkerhet:" Grid.Column="0" Grid.Row="1" Margin="0 5" />
<TextBlock Text="Den här filen kom från en annan dator och kan ha blockerats för att skydda datorn." TextWrapping="Wrap"
Grid.Column="1" Grid.Row="1" Margin="0 5 10 0" Grid.ColumnSpan="2" />
<CheckBox Content="Avblockera" Grid.Column="3" Grid.Row="1"
VerticalContentAlignment="Top" Margin="0 10" />
</Grid>
</StackPanel>
</ScrollViewer>
</Border>
</Grid>
<!-- Status bar -->
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" Height="20">
<TextBlock Text="Status bar" VerticalAlignment="Center" HorizontalAlignment="Left"
Margin="12 0 0 6" FontWeight="Bold"
Width="60" FontSize="12" />
<ProgressBar Margin="10 0 0 4" Value="69" VerticalAlignment="Center"
Width="200" Height="12"/>
</StackPanel>
</Grid>
</Window>
C# Code:
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
namespace RFAnalyzerMain
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public OpenFileDialog openBrowser = new OpenFileDialog();
public MainWindow()
{
InitializeComponent();
}
private void OpenExplorerAndShowFilesAndFolderName_Click(object sender, RoutedEventArgs e)
{
int i = 1;
string filePath = openBrowser.FileName;
Nullable<bool> result = openBrowser.ShowDialog();
if (result == true)
{
while (true)
{
char foundChar = openBrowser.FileName[openBrowser.FileName.Length - i];
i += 1;
if (foundChar == '\\')
{
break;
}
else
{
filePath = openBrowser.FileName.Remove(openBrowser.FileName.Length - i);
}
}
folderPath.Text = filePath;
ResultFilesDataGrid.ItemsSource = new DirectoryInfo(folderPath.Text).GetFiles();
}
}
private void ResultFilesDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
var cellInfo = ResultFilesDataGrid.CurrentCell;
var content = cellInfo.Column.GetCellContent(cellInfo.Item);
string filePath = folderPath.Text;
CultureInfo seCulture = new CultureInfo("sv-SE");
FileInfo oFileInfo = new FileInfo(filePath + "\\" + content.DataContext.ToString());
fileSize.Text = oFileInfo.Length.ToString("N0", seCulture) + " byte";
pathToFolder.Text = oFileInfo.Directory.ToString();
fileCreated.Text = oFileInfo.CreationTime.ToString();
fileChanged.Text = oFileInfo.LastWriteTime.ToString();
fileUsed.Text = oFileInfo.LastWriteTime.ToString();
string readableName = oFileInfo.Extension.ToUpper();
if (readableName.Length != 0)
{
readableName = readableName.Remove(0, 1);
fileType.Text = readableName + "-fil (" + oFileInfo.Extension.ToString() + ")";
}
else
fileType.Text = null;
}
}
}
The NullReference comes up at this part: var content = cellInfo.Column.GetCellContent(cellInfo.Item);
This is my first post, sorry if it's not that good, and I apologies for the Swedish language. And I know the code is messy, I plan on cleaning it up after I get this problem done.