none
How to bind to a single element of a ObservableCollection? RRS feed

  • Question

  • I have a ObservableCollection and I want to return a value from one entry.

    Lets say my collection is the price of fruits:

    Item      Price

    Apple    $1.45

    Banana $2.34

    Carrot   $0.89

    And I want a textblock that is bound to the price of the Apples.  How would I go about that?
    My actual project is using values that could changed every second. And I will have many textblocks looking for their own values.

    Tuesday, May 29, 2012 3:55 AM

Answers

  • If you have a collection with different Item Types in it... And you have individual controls say one for Apple, one for Bananna then you have to provide each with their own binding. That binding would come from LINQ (if you want and would look like this);

    var apples = Collection.Where(p=>p.Item = "Apple");
    TextBoxApples.DataContext = apples;
    

    You would then set the Textbox.Text propery to "Binding  path=Item" in XAML.  The price field would be bound like this "Binding path = Price".

    Typically the datacontext would be on the next higher level container such as a grid or a stackpanel.  That way both the Textboxes in the grid would use the same collection to bind to, the only difference would be the path.


    JP Cowboy Coders Unite!

    Monday, June 4, 2012 5:37 PM

All replies

  • Hi,

     Please find the code here,

    <TextBlock Width="180" Text="{Binding RelativeSource={RelativeSource Self}, Path=DataContext.Item }" DataContext="{Binding myDisplayInfo}" ></TextBlock>


    Sai Kumar K (MCP)
    Blog: Sai's Stuff.
    WebSite: SantoshTechnologies.

    Tuesday, May 29, 2012 5:54 AM
  • Hi,
    What you need to fetch out the item from Observable collection and assign to a property in your view model say "AppleProperty". Use this "AppleProperty" to bind with your TextBlock.
    Here is Sample code.

    //Get apple item from FruitList and assign to AppleProperty
    AppleProperty = FruitList.FirstOrDefault(item => item.CategoryName.Equals("apple"));
    //Collection changed handle for refresh the Apple Property
    FruitList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(AllowList_CollectionChanged);
    void AllowList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
         //set the Apple property
         AppleProperty = FruitList.FirstOrDefault(item => item.CategoryName.Equals("apple"));
    }
            
    CFruit _apple;
    public CFruit AppleProperty
    {
       get { return _apple; }
       set
       {
         _apple = value;
       }
    }
    Hope this will help you. Have a nice day.
    Tuesday, May 29, 2012 5:59 AM
  • Hi DChiShaggy,

    What is the meaning about "single element ", is it a single item, if so you could set path to that item index, for example collection[0].property, refer to this msdn document:

    http://msdn.microsoft.com/en-us/library/system.windows.data.binding.path.aspx

    If you want to binding to a collection, you could add a binding converter, and find the value you want, and then return this value, refer to binding converter:

    http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converter.aspx

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by LeeCampbell Wednesday, May 30, 2012 10:39 AM
    Wednesday, May 30, 2012 6:00 AM
    Moderator
  • What is the meaning about "single element ", is it a single item,

    if so you could set path to that item index, for example collection[0].property,

    refer to this msdn document:


    That is roughly what I am trying to do.  My issue is I will not know the index number.  Today the Banana could be [1], but tomorrow could be [2] depending on how the source data is filled and items are added and removed.

    So I wish I could do something like collection["Carrot"].Price and then bind a textblock to that.


    • Edited by DChiShaggy Monday, June 4, 2012 1:27 PM
    Monday, June 4, 2012 1:21 PM
  • If you have a collection with different Item Types in it... And you have individual controls say one for Apple, one for Bananna then you have to provide each with their own binding. That binding would come from LINQ (if you want and would look like this);

    var apples = Collection.Where(p=>p.Item = "Apple");
    TextBoxApples.DataContext = apples;
    

    You would then set the Textbox.Text propery to "Binding  path=Item" in XAML.  The price field would be bound like this "Binding path = Price".

    Typically the datacontext would be on the next higher level container such as a grid or a stackpanel.  That way both the Textboxes in the grid would use the same collection to bind to, the only difference would be the path.


    JP Cowboy Coders Unite!

    Monday, June 4, 2012 5:37 PM
  • Hi DChiShaggy,

    How about your issue?

    best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 8, 2012 4:35 AM
    Moderator