I am having a issue with data binding from a instantiated class in project A to Project B. The data binding "seems" to be working correctly; however, I am definitely getting a different instantiation of ClassA.
This is the ClassA - which is a class in a different Project. ClassA is a console application and has a main.
public class ClassA: INotifyPropertyChanged
{
private string _partNumber;
private Guid _guid = Guid.NewGuid ( );
// Make a singleton of this class so other classes have to request an instance
private static readonly ClassA _Instance = new ClassA ( );
//
//.. Read in xml file "This executes properly - I've checked"
//
public static ClassA GetInstance ( )
{
return _Instance;
}
public string PartNumber
{
get
{
if ( string.IsNullOrEmpty ( _partNumber ) )
{
Console.WriteLine("Got PartNumber: " + _partNumber + " GUID: " + _guid);
return "Empty";
}
Console.WriteLine("Got PartNumber: " + _partNumber + " GUID: " + _guid);
return _partNumber;
}
set
{
if ( value == null )
{
throw new ArgumentNullException ( "value" );
}
Console.WriteLine("Set Partnumber: " + value + " GUID: " + _guid);
_partNumber = value;
}
}
}
This is the MainWindow.xaml.cs which is in a different project from ClassA:
public partial class MainWindow : MetroWindow
{
public MainWindow ( )
{
InitializeComponent ( );
DataContext = ClassA.ClassAInstance;
}
...
}
This is the XAML code that calls the binding
<controls:MetroWindow ... ...
<TextBlock Grid.Row="1" Grid.Column="1"
Style="{StaticResource LblTextData}"
Text="{Binding PartNumber, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"/>
... ...
</controls:MetroWindow>
When I run the program I run Console.WriteLine() to verify the data and GUID's. The data printed from ClassB is null and the GUID's are totally different from ClassA. My question is how can I get the same class instance - rather then a totally different instance?
Here is the Console output:
Set Partnumber: 736543 GUID: 360a6dcc-a719-4ad0-a929-6f3ed09eefb2
Get PartNumber: GUID: 2405b370-0806-4f41-91c6-0fdd6f894077
I am also getting this error at runtime:
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=PartNumber; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')