1

I made work the following code which call an int in a Singleton:

        <ListView
            SelectedIndex="{Binding Path=DBProviderInstance.CurrentPartIndex, Mode=TwoWay}"
            Margin="20,20,20,0"
            Grid.Row="0"
            Name="ListViewItem"
            SelectionMode="Single"
            Background="#DCE3E5">

        public DBProvider DBProviderInstance
        {
            get { return DBProvider.Instance; }
        }

I had to introduce a session logic so now I use an array of singleton. The xaml.cs code is now like this:

        public static DBProvider DBProviderInstance
        {
            get
            {
                string session = DBProvider.GetSession().FirstOrDefault();
                if (DBProvider.GetInstance(session) == null)
                    return null;
                return DBProvider.GetInstance(session);
            }
        }

My problem is that now it doesn't react like if it were not binded. However in the launch of the sln, the value is get. Why would this not work?

  • As a note, checking `if (DBProvider.GetInstance(session) == null)` and then returning null is entirely redundant. `return DBProvider.GetInstance(session);` already returns null in that case. – Clemens Aug 24 '20 at 12:47
  • How is `CurrentPartIndex` defined? Are you sure that `public static DBProvider DBProviderInstance` always returns the same DBProvider instance? – Clemens Aug 24 '20 at 12:58
  • Thx Clemens, it is indeed. Sinatr, no it doesn't because I don't need the static. I removed it and it doesn't work. The difference is mainly that Instance is no get by a method GetInstance(). – user10722553 Aug 24 '20 at 13:00
  • Then the correct syntax would be `Text="{Binding Path=(local:DBProvider.DBProviderInstance).CurrentPartIndex}"` - where `local` might be replaced by the appropriate XAML namespace. – Clemens Aug 24 '20 at 13:04
  • Besides that, it seems still unnecessary to make the property static. The following should work as well (with the original Binding Path): `public DBProvider DBProviderInstance { get { return DBProvider.GetInstance(DBProvider.GetSession().FirstOrDefault()); }}` – Clemens Aug 24 '20 at 13:22
  • Or shorter: `public DBProvider DBProviderInstance => DBProvider.GetInstance(DBProvider.GetSession().FirstOrDefault());` – Clemens Aug 24 '20 at 13:23
  • It can't find the DBProvider reference. Also, DBProviderInstance is in the MainWindow.xaml.cs. I tried few thing with or without the namespace, or the "local". – user10722553 Aug 24 '20 at 13:28
  • The static property path above assumes that the static DBProviderInstance property is declared in the DBProvider class (you haven't told us anything about that). If that is not the case, use the appropriate class name. – Clemens Aug 24 '20 at 13:35
  • I removed the static as said above and I'm in the MainWindow.xaml.cs. "DBProviderInstance.CurrentPartIndex" was working when it was "public DBProvider DBProviderInstance { get { return DBProvider.Instance; } } – user10722553 Aug 24 '20 at 13:41
  • Not sure what you're going to tell us. IMO the property doesn't need to be static, and the original Binding should just work. – Clemens Aug 24 '20 at 13:57
  • I found the problem. The value of DBProviderInstance was null on initialisation and was never send back to the wpf after being set. – user10722553 Aug 25 '20 at 07:05

0 Answers0