0
votes

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?

2

2 Answers

0
votes

Set the source property for the TextBlock in the setter of the source property of the SelectedRec property, e.g.:

public AllTexts SelectedRec
{
    get { return p_SelectedRec; }
    set
    {
        if (p_SelectedRec != value)
        {
            p_SelectedRec = value;
            RaisePropertyChanged("SelectedRec");
            if (p_SelectedRec != null)
                Text1 = p_SelectedRec.SomeStringPropertyOfTheAllTextsClass;
        }
    }
}

Also note that you should bind to either the SelectedItem or the SelectedValue property of the ComboBox:

<ComboBox x:Name="cmbSelectText" ItemsSource="{Binding ALT}" DisplayMemberPath="Id" SelectedItem="{Binding SelectedRec}" />
0
votes

XAML:

    <ComboBox SelectedItem="{Binding SelectedText}" HorizontalAlignment="Left" Margin="50,65,0,0" VerticalAlignment="Top" Width="120" />
    <TextBlock Text="{Binding SelectedText}" HorizontalAlignment="Left" Margin="56,108,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>

c#:

    private string _SelectedText;

    public string SelectedText
    {
        get { return _SelectedText; }
        set { _SelectedText = value; OnPropertyChanged("SelectedText"); }
    }

    public MainWindow()
    {
        InitializeComponent();
    }