In my view,I have a Combobox and a TextBlock. The TextBlock is recieving value via a DataBinding set on Text Property in XAML from a ViewModel named "MyViewModel" upon FormLoad.
The Combobox has an ItemSource and a SelectedItem,which are bounded to properties in the same ViewModel ie "MyViewModel"
Now,when i change the Selection of Combobox from the View,I want a member of the Combobox's SelectedItem's object to be set in the TextBlock.
How can i do this?ie is there a way that somehow i can change/toggle the TextBlock's Text's DataBinding property to different sources/properties from "MyViewModel" depending upon my situation at runtime? or how can this be best resolved?
public class AllTexts
{
public int ID {get;set;}
public string Text1{get;set;}
public string Text2{get;set;}
}
public class MyViewModel:INotifyPropertyChanged//Assume the interface has been implemented.
{
private string p_Text1;
private AllTexts p_SelectedRec;
public string Text1
{
get{return p_Text1;}
}
set
{ if(p_Text1!=value)
{
p_Text1=value;
RaisePropertyChanged("Text1");
}
}
public List<AllTexts> ALT;
public AllTexts SelectedRec
{
get{return p_SelectedRec;}
}
set
{ if(p_SelectedRec!=value)
{
p_SelectedRec=value;
RaisePropertyChanged("SelectedRec");
}
}
public MyViewModel()
{
ALT=new List<AllTexts>();//Assume this List gets populated
}
}
MainWindow.Xaml(View)
<ComboBox x:Name="cmbSelectText" ItemsSource="{Binding ALT}" DisplayMemberPath="Id" SelectedValuePath="Id" SelectedItem="{Binding SelectedRec}" SelectedValue="{Binding SelectedRec.Id,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock x:Name="txtTextSpecimen" Text="{Binding Text1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
How can i change/toggle this binding between Combox's SelectedItem's property named "Text2 " and ViewModel's property named "Text1" at runtime?