Reputation: 709
I've seen enough examples with binding to property A
with a datacontext where A
exists in the viewModel class. Now what if in the viewModel I do not have any property A
, instead I create some calss B that contains property A
, then how to set up a binding here?
Let's say in xaml<TextBlock Text="{Binding Bid}"..>
and In the viewModel's constructor I set up
Quote b = new Quote();
HttpClient.QuoteMap.Add(1,b);
HttpClient.Socket.reqMktdata(1,contract,..)
So b
keeps updating its Bid
and Ask
... The thing is I don't see how to set a binding to b
's Bid
. For listview
or DataGrid
I can see how to do it as there's a property called itemsource
to specify the data binding source and for each column different property is bind to any property if needed.
Upvotes: 0
Views: 263
Reputation: 31616
In your viewmodel create a property A
which has the notify event on its change, but get the data from the B
instance. If B
has INotifyPropertyChanged, then subscribe to those changes and when B
event fires that a change has occurred, post the notified change for your property A
such as
OnPropertyChanged("A");
That way you can have related data which exists elsewhere but still updates the view accordingly.
This will update when the B property Data changes, to the property A
on the MVVM.
class MyMVVM : INotifyPropertyChanged
{
private TheBClass B { get; set { OnPropertyChanged("A"); } }
public string A
{
get { return B.Data; }
set { OnPropertyChanged("A"); }
}
public MVVM(TheBClass bInstance;)
{
B = bInstance;
B.PropertyChanged += (sender, args) =>
{
if (args.PropertyName == "Data")
OnPropertyChanged("A");
};
}
}
Upvotes: 0
Reputation: 45096
The class itself needs to be a property.
<TextBlock Text="{Binding Path=MyClassToBind.PublicProperty}"
private MyClass myClass = new MyClass();
public MyClass MyClassToBind
{ get { return myClass; } }
Upvotes: 1