I've created a user interface with a save button to allow user export the current configuration into the desired location using a .json file.
This is my button:
<Button Height="25" Margin="5, 0, 0, 0" Width="50" Command="{Binding SaveConfigurationCommand}">
<Button.Content>
<Border CornerRadius="5">
<TextBlock Text="Save" FontSize="10" Foreground="White"/>
</Border>
</Button.Content>
</Button>
And this is my viewModel:
private DelegateCommand saveConfigurationCommand;
public DelegateCommand SaveConfigurationCommand =>
saveConfigurationCommand ?? (saveConfigurationCommand = new DelegateCommand(ExecuteSaveConfigurationCommand));
void ExecuteSaveConfigurationCommand()
{
try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "json files (*.json)|";
saveFileDialog.InitialDirectory = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\..\\Configuration"));
if (saveFileDialog.ShowDialog() == true)
{
Configuration configuration = new Configuration
{
Weight = total_weight,
Animals = new List<Container>(animals),
Items = new List<Item>(Items)
};
File.WriteAllText(saveFileDialog.FileName, Newtonsoft.Json.JsonConvert.SerializeObject(configuration));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Save configuration error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
The problem is, saveFileDialog.FileName is always empty. What I'm doing wrong?