Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I set My modelView in data context like the code below :
but I get this one error parameter name path cannot be null in this line :
<local:ViewModel/>

I limit the problem in the Text file reader used in my public model view ,
Cause when I changed the configuration management.appsetting with a simple string path the error has disappear .

My question is , how can I set My view model In data context while I'm using app config/setting configurataion management in my view model constructor ?

Thank you for you help and attention.

What I have tried:

<pre><Window x:Class="BancProduction.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:BancProduction"
        Title="MainWindow" Height="432.4" Width="687.4">
      
    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>


This is my first partcode of my Viewmodel:

namespace BancProduction
{
    class ViewModel : INotifyPropertyChanged
    {

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
    

        #endregion
  
        public void RaisePropertyChanged([CallerMemberName] string str = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(str));
            }
        }

 public search SearchCritaire
        {
            get
            {
                return Search;
            }
            set
            {
                if (value != Search)
                {
                    Search = value;
                    RaisePropertyChanged();
                }
            }
        }
     
        private ObservableCollection<search> famille;
   
        public ObservableCollection<search> Famille { get { return famille; }
            set { if (value != famille)
                {
                 famille = value; RaisePropertyChanged(); }
            
            } }

             public ViewModel()
        {   
          //this is the line reader that makes the error   
         string[] lineOfContents = File.ReadAllLines(ConfigurationManager.AppSettings["FiletextPath"]);

            MyList= new ObservableCollection<search>();           
           foreach (var line in lineOfContents)
           {
       string myline = line ;
                SearchCritaire = new search() { banc = line};
               

                MyList.Add(SearchCritaire);

            }

          }

this is my app config :

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="FiletextPath" value="D:\\Application Data\\Famille.txt"/>
    </appSettings>
</configuration>


I'm using the the binding method with view model , so I'm not working with the code behind method :

namespace BancProduction
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
}

}
Posted
Updated 19-Sep-19 6:22am
v2

Try setting a design-time context:
XAML
<Window x:Class="BancProduction.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" mc:Ignorable="d"
    xmlns:local="clr-namespace:BancProduction"
    Title="MainWindow" Height="432.4" Width="687.4"
    d:DataContext="{d:DesignInstance local:ViewModel}"
>
    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>
Using Design-time Databinding While Developing a WPF User Control[^]

You should also modify your viewmodel constructor so that it doesn't fail if the path doesn't exist:
C#
public ViewModel()
{
    MyList = new ObservableCollection<search>();
    
    string path = ConfigurationManager.AppSettings["FiletextPath"];
    if (!string.IsNullOrEmpty(path) && File.Exists(path))
    {
        foreach (string line in File.ReadLines(path))
        {
            MyList.Add(new search { banc = line });
        }
    }
}
 
Share this answer
 
Comments
EM_Y 20-Sep-19 4:58am    
Thank you Richard

this code added make the difference . ^^

 if (!string.IsNullOrEmpty(path) && File.Exists(path))
    { }
First, can you post your config file? Are you sure that there is an app setting called 'FiletextPath'

Next, break down this line into two
string[] lineOfContents = File.ReadAllLines(ConfigurationManager.AppSettings["FiletextPath"]);

so that it becomes
var path = ConfigurationManager.AppSettings["FiletextPath"]
string[] lineOfContents = File.ReadAllLines(path);

Then see which line throws the error.
 
Share this answer
 
Comments
EM_Y 19-Sep-19 4:08am    
Sorry but I didn't mention that my code debug perfectly even with this error , the objective of the code I set in my constructor is to fill my combobox with my file text lines , and it works ! that means that my path config is correct .

the problem is may be the data context needs specific format to define my view model when I use the app config reader in my constructor !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900