Wpf equivalent of ValueMember/DisplayMember

6 posts, 0 answers
  1. jholt
    jholt avatar
    21 posts
    Member since:
    May 2007

    Posted 29 Oct 2008 Link to this post

    Is there a way to bind to lookup values in a gridview?

    What I'm trying to achieve is nothing more complex than in Windows Forms ValueMember and DisplayMember:
    This code is from the RadControls for WinForms example:Column Types
              
      //add new lookup column             
      GridViewLookUpColumn lookUpColumn = new GridViewLookUpColumn();             
      lookUpColumn.HeaderText = "Look up";                      
      lookUpColumn.FieldName = "PersonID";             
      lookUpColumn.DataSource = this.employeesBindingSource;             
      lookUpColumn.ValueMember = "EmployeeID";             
      lookUpColumn.DisplayMember = "LastName"

    Given the classes below where the GridView binds to PriceList which is a collection of Price(s), I want the CommodityId column to be a combo box which, when in edit mode gives me a list of Commodity entities showing the Description text and when in view  mode shows me the Description text, but saves the Commodity.Id value back to AppPresenter.PriceList.

    By including DataMemberPath in the CommodityId field of the DataView, I am able to display Commodity.Description. However, the selected value from the combo is not reflected in the underlying datasource (PriceList).
    If I remove DataMemberPath from the markup - it is clear to see that the PriceList is never updated with the newly selected value.

    I'm guessing that the reason for this is that I am not binding Commodity.Id (in the lookup) to CommodityId in the GridView. So my question is - how do I do this binding? The example for DataBinding doesn't seem to cover this case.


    public class Commodity() 
        public int Id {get;set;} 
        public string Description {get;set;} 
         
        public override ToString() 
        { 
            return Description; 
        } 
     
    public class Price 
        public int Id {get;set;} 
        public DateTime Date {get;set;} 
        public int CommodityId {get;set;} 
        public EntityRef<Commodity> Commodities {getset;} 
    public class AppPresenter 
        public ObservableList<Price> PriceList {get;} 
     
    public class Shell 
        pubic Shell() 
        private IList<Commodity> _commodityList; 
        { 
            InitializeComponents();  
             
            _commodityList = repository.GetCommodites(); 
            colCommodityEditorSettings = new ComboBoxEditorSettings { ItemsSource = _commodityList};             
                colCommodity.EditorSettings = colCommodityEditorSettings; 
            } 

     
     <telerik:RadGridView Grid.ColumnSpan="4" Grid.Row="3" Margin="5" x:Name="radGridView1" ColumnsWidthMode="Auto" ItemsSource="{Binding Path = PriceList}"  
                                 AutoGenerateColumns="False"
                <telerik:RadGridView.Columns> 
                    <telerik:GridViewDataColumn DataType="{x:Null}" HeaderText="ID" UniqueName="Id" x:Name="colId" /> 
                    <telerik:GridViewDataColumn DataType="{x:Null}" HeaderText="Date" UniqueName="Date" IsAutoGenerated="False" IsVisible="True" x:Name="colDate" /> 
                    <telerik:GridViewDataColumn DataType="{x:Null}" HeaderText="Hidden" UniqueName="IsHidden" x:Name="colIsHidden" /> 
                    <telerik:GridViewDataColumn DataType="{x:Null}" HeaderText="Commodity" UniqueName="CommodityId" x:Name="colCommodity" DataMemberPath="Commodities.Description">   
    </telerik:RadGridView>                 
     
     

    Many thanks in advance for your help
    Jeremy Holt

  2. Bronwen
    Bronwen avatar
    54 posts
    Member since:
    Sep 2008

    Posted 30 Oct 2008 Link to this post

    Hi jholt,

    Check out the last reply on the question I raised here: http://www.telerik.com/community/forums/thread/b311D-bgkmkk.aspx

    It provides an example of this.

  3. Download a free trial of UI for WPF
  4. jholt
    jholt avatar
    21 posts
    Member since:
    May 2007

    Posted 30 Oct 2008 Link to this post

    Hi,

    Support directed me to the link and I downloaded the file. It just strikes me that it is an incredibly complicated way to do something that should really be a basic requirement in any kind of datagrid - namely do a lookup by foreign key. In fact if one is going to use any embedded control in a cell the requirement is always going to be the same - bind the result from the embedded control to the underlying data of the field in the grid.

    I'm very very new to Wpf - so maybe it's just me - I still have to think very hard about Bindings and Templates....

    Regards
    Jeremy


  5. Bronwen
    Bronwen avatar
    54 posts
    Member since:
    Sep 2008

    Posted 30 Oct 2008 Link to this post

    I agree...lots of code for something that I'm going to do ALL the time. Would have expected a way to just add the binding properties I needed.

    Hoping that next version/s will improve this.

  6. jholt
    jholt avatar
    21 posts
    Member since:
    May 2007

    Posted 31 Oct 2008 Link to this post

    +1 from me

  7. Dimitar Dobrev
    Admin
    Dimitar Dobrev avatar
    44 posts
    Member since:
    May 2013

    Posted 31 Oct 2008 Link to this post

    Hello, Jeremy, Branwen,

    Unfortunately, we are not able to provide a simpler solution for this problem right now. We have in our feature list the ability to create your editors from XAML. Your feedback gives higher priority to this feature and will surely help it be included in one of our future releases.
     
    Regards,

    Dimitar Dobrev,
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.

Back to Top
Download a free trial of UI for WPF