Welcome to the Xceed Community | Help
Community Search  
More Search Options

Binding by ID

Sort Posts: Previous Next
  •  04-23-2013, 4:12 AM Post no. 33264

    Binding by ID

    Hi all,

    I have the following situation. I am using a DataGrid to display a list of Boxes. Each Box has a ProductID property. I also have a list of Products, each of which has an ID and a Name.

    I want to 1) display a product Name instead of the ProductID, and 2) allow the user to select a Product from a ComboBox in the CellEditor, and change Box.ProductID accordingly.

    So far, I've been able to do #1 by using DisplayMemberBindingInfo with a Converter that turns ProductIDs into product names.

    However, I'm stumped on #2. I've tried putting this in the cell editor template:

                                    <ComboBox IsEditable="True" IsTextSearchEnabled="True" DisplayMemberPath="Name"
                                              ItemsSource="{Binding Client.Products, Source={x:Static local:App.Instance}}"
                                              SelectedValue="{Binding ProductID}"
                                              SelectedValuePath="ID"/>

    but it won't work, I think because at this point the source of the Binding of SelectedValue is not a Box, but rather a product name. So I've changed it to:

                                    <ComboBox IsEditable="True" IsTextSearchEnabled="True" DisplayMemberPath="Name"
                                              ItemsSource="{Binding Client.Products, Source={x:Static local:App.Instance}}"
                                              SelectedValue="{xcdg:CellEditorBinding}"
                                              SelectedValuePath="Name"/>

    This results in the ConvertBack method of my Converter getting called with the selected product name, which won't work either (as I can't map back from product names to product IDs).

    I realised that using DisplayMemberBindingInfo with a Converter is probably not the right way to do this, as it means that in the CellEditor I'm stuck with a product name. But I can't figure out how to do it otherwise. Help?
  •  04-23-2013, 1:29 PM Post no. 33266 in reply to 33264

    Re: Binding by ID

    Hi Filippo,

    If I understood your requirements properly, using the Column's ForeignKeyConfiguration property should help you

    to achieve what you are looking for. A simple code snippet which can be used as an example of using

    ForeignKeyConfiguration to display a Car object, which has an ID and Desc properties, could be:

    Within the Resources section:

    <xcdg:ForeignKeyConfiguration x:Key="CarsForeignKeyConfiguration"
                                          ItemsSource="The Binding source of your ComboBox"
                                          ValuePath="CarCode" />

            <DataTemplate x:Key="CarTemplate">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Desc}" Width="75" />
                    <TextBlock Text="{Binding CarCode}" />
                </StackPanel>
            </DataTemplate>

    Then your Column's definition could be:

                    <xcdg:Column FieldName="CarCode" Title="Cars"
                                 ForeignKeyConfiguration="{StaticResource CarsForeignKeyConfiguration}"
                                 CellContentTemplate="{StaticResource CarTemplate}"/>


    Fawzi Bablli
    Developer Technical Support
    Xceed Software Inc.
  •  04-25-2013, 6:10 AM Post no. 33269 in reply to 33266

    Re: Binding by ID

    Hi Fawzi,

    Thanks for your answer. Here's what I'm doing now:

    In the Resources:

                     <xcdg:ForeignKeyConfiguration x:Key="ProductsForeignKeyConfiguration" ItemsSource="(a list of Products)" ValuePath="ID"/>

    In the columns:

                     <xcdg:Column FieldName="Product" ForeignKeyConfiguration="{StaticResource ProductsForeignKeyConfiguration}">
                        <xcdg:Column.CellContentTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}"/>
                            </DataTemplate>
                        </xcdg:Column.CellContentTemplate>
                    </xcdg:Column>

    This is not working. The editor seems to work (it displays a ComboBox with product names and updates the Product field in the Box correctly with the selected Product's ID). But the normal display is not working; it's not showing anything. I've tried changing the TextBlock binding to simply {Binding} to see what it was binding against, and it displays the Product.ID. So it seems that the context of the TextBlock is not Product (from which I could get the Name), but Product.ID.

    By the way, is this method doing a linear search on the Product list to find the foreign key? Any way to get it to use an index? The list could be pretty big, and the Box table could be pretty big too.
  •  04-25-2013, 10:13 AM Post no. 33270 in reply to 33269

    Re: Binding by ID

    Hi Filippo,

    Could you send me a simple stand-alone sample project which reproduces the "normal display" issue so that I can help you better. Please send it to:

     support@xceed.com 


    Fawzi Bablli
    Developer Technical Support
    Xceed Software Inc.
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.