locked
How do I conditionally bind in XAML? (Explains in post) RRS feed

  • Question

  •  Hello (First of all I'm not to sure which thread I would post binding related questions in.. but this seemed like the most generic so:)

     

    I have a Silverlight page, with  an MS SQL express DB, then an ADO EFM, then finally a WCF RIA services. How do I bind a single control in xaml (e.g. a Textbox) to a specific entity -if any- in my Domain Data Source that meets some specific condition? (I think the answer is IValueConverters, but I really don't know tbh)

    e.g. If I where to manually set it in codebehind I would go:

     
    1    Foreach (ChildEntity childEntity in parentEntity)
    2 {
    3 if (childEntity = "mycondition") {TextBox.DataContext = childEntity; break; }
    4 }

     







    Just to fully explain my scenario (it is a bit more complicated then the example above)
    I've sucesfully created a page that shows some Parent Table "Jobs", the Job has a FK in it that links to a "Bill" and some of the JobItems have a FK Line, which links to "BillItem.Line" (which is a child table of Bill). And I'm trying to display the Job, with it's BillLine -if it exists-.

    Job.BillId -> Bill{ BillId }
    Job.Cost1LineId -> Bill{ BillId }.BillItem { BillId, LineId }

    Tuesday, May 11, 2010 2:49 AM

Answers

  • If you implement it in a MVVM pattern. you need to have another class for ViewModel then set this as your Datacontext.

    example : public void Main() { this.DataContext = new ViewModel(); }

    In the ViewModel, you expose the needed bindable properties each for parent and child..

    example : public string JobName { get... set { .. OnPropertyChanged("JobName");} }

    public string BillName { get... set { .. OnPropertyChanged("BillName");} }

    you bind your textbox in the XAML to these 2 properties.

    Then somewhere in the ViewModel, you need to trigger the load of your parent which is the Job. you can include the child in the load so that you don't have to do another load operation.

    LoadOperation lo = domaincontext.load(domaincontext.GetJobQuery());

    lo.Complete += (s,e) => { JobName = domainContext.Jobs.First().Name;

    BillName = domainContext.Jobs.First().Bills.First().Name;

    };

    Tuesday, May 11, 2010 3:59 AM

All replies

  • check this link

    Tuesday, May 11, 2010 3:55 AM
  • If you implement it in a MVVM pattern. you need to have another class for ViewModel then set this as your Datacontext.

    example : public void Main() { this.DataContext = new ViewModel(); }

    In the ViewModel, you expose the needed bindable properties each for parent and child..

    example : public string JobName { get... set { .. OnPropertyChanged("JobName");} }

    public string BillName { get... set { .. OnPropertyChanged("BillName");} }

    you bind your textbox in the XAML to these 2 properties.

    Then somewhere in the ViewModel, you need to trigger the load of your parent which is the Job. you can include the child in the load so that you don't have to do another load operation.

    LoadOperation lo = domaincontext.load(domaincontext.GetJobQuery());

    lo.Complete += (s,e) => { JobName = domainContext.Jobs.First().Name;

    BillName = domainContext.Jobs.First().Bills.First().Name;

    };

    Tuesday, May 11, 2010 3:59 AM