Home Forums WPF controls Xceed DataGrid for WPF Example for a ComboBox cell editor in code-behind

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • Xceed Support
    Member
    Post count: 5658
    #21679 |

    Hi,
    I’m looking for an example of a ComboBox cell editor in C#.

    Thanks in advance.

    Imported from legacy forums. Posted by Simon (had 8471 views)

    Xceed Support
    Member
    Post count: 5658

    Can you give more information on the nature of the ComboBox you wish to create?

    Is the content of the ComboBox a static list of element?
    Should the ComboBox be populated according to a data set relationship?

    Imported from legacy forums. Posted by Marcus [Xceed] (had 574 views)

    Xceed Support
    Member
    Post count: 5658

    the ComboBox Should be populated according to a list of item for a DB.

    In the example: DynamicEditorDisplay,
    there is a Cell Editor: categoryEditor
    and a CellContentTemplate: categoryCellDataTemplate, this one is easy to reproduce.

    But the categoryEditor, is not very easy to reproduce, I’m having problem with:

    SelectedValue=”{xcdg:CellEditorBinding}”>

    Thanks for the help.

    Imported from legacy forums. Posted by Simon (had 421 views)

    Xceed Support
    Member
    Post count: 5658

    Here is the method reproduce “in-code” the “SelectedValue = {xcdg:CellEditorBinding}”.

    <code>
    CellEditorBinding cellEditorBinding = new CellEditorBinding();

    myComboBox.SetBinding( SelectedValueProperty, cellEditorBinding.ProvideValue( null ) as BindingBase );
    </code>

    Also, for your scenario, you can consider translating the XAML template, available in the SolidFoundation sample, which demonstrate how you can quickly and easily create a ComboBox editor that maps to another table. This template makes usage of a small utility class we created for that purpose: DataTableDictionary

    Imported from legacy forums. Posted by Marcus [Xceed] (had 742 views)

    Xceed Support
    Member
    Post count: 5658

    Works great thanks!!

    Imported from legacy forums. Posted by Simon (had 343 views)

    Xceed Support
    Member
    Post count: 5658

    I have another problem, in my combo box, I have set the SelectedValuePathProperty.

    When I’m in the grid and I leave the combo box to edit something else, the text in the cell of where the combo box was, is the return value of SelectedValuePathProperty.What I mean is, I use a guid as selected value, so in the cell text, there is a string of the guid.

    Does this occurs because I have set the SelectedValuePathProperty of the combo box?
    Or am I missing something again?

    Thanks for your help.

    Imported from legacy forums. Posted by Simon (had 599 views)

    Xceed Support
    Member
    Post count: 5658

    Am I right to assume that the content of the column you are placing the ComboBox editor on is in fact composed of GUIDs, with are the keys used to map the relation to another table?

    If the answer is yes, then I suppose that you were able to display the appropriate information in the ComboBox using a DataTemplate ( on the ComboBox.ItemTemplate property ) that takes the GUID and displays the information found in your other table instead of the GUID?

    If I’m right in both those assumptions, then you need to apply the same template as the CellContentTemplate for the column where the GUID is present…

    Imported from legacy forums. Posted by Marcus [Xceed] (had 584 views)

    Xceed Support
    Member
    Post count: 5658

    Thanks it works, but the only problem is that I have a List<Entity> and I put this list in the combo box ItemsSource. I set the DisplayMemberPathProperty and the SelectedValuePathProperty, when I set CellContentTemplate has the same CellEditor, It’s incredibly slow, I have more then 400 entries in the list. It seems like every time i click on the cell with combo box, the combo box loads the list and when I edit another cell, I still can see the combo box, is there a way to see only the selected value as a string in a textblock?

    Thanks in advance.

    Imported from legacy forums. Posted by Simon (had 653 views)

    Xceed Support
    Member
    Post count: 5658

    In fact, you should not set the CellContentTemplate to the CellEditor.EditTemplate.

    When you built your ComboBox editor, you probably assigned a ItemTemplate to the ComboBox, it is that DataTemplate you should set on the CellContentTemplate property.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 386 views)

    Xceed Support
    Member
    Post count: 5658

    That is exactly what I have done and it still doesn’t work:

    private Column CreateDropDownListTemplate( Column aColumn, string aPropertyToBind, ColumnListAttribute aColumnListAttribute )
    {
    CellEditor cellEditor = new CellEditor();
    FrameworkElementFactory comboBox = new FrameworkElementFactory( typeof( ComboBox ) );

    comboBox.SetValue( ComboBox.NameProperty, “comboBoxExTemplate” );
    comboBox.SetValue( ComboBox.BackgroundProperty, Brushes.Transparent );
    comboBox.SetValue( ComboBox.BorderThicknessProperty, new Thickness( 0, 0, 0, 0 ) );
    comboBox.SetValue( ComboBox.DisplayMemberPathProperty, aColumnListAttribute .DisplayMember );
    comboBox.SetValue( ComboBox.SelectedValuePathProperty, aColumnListAttribute .ValueMember );

    //ItemTemplate
    FrameworkElementFactory textBlockEx = new FrameworkElementFactory( typeof( TextBlock ) );
    textBlockEx.SetValue( TextBlock.TextProperty, new Binding( aColumnListAttribute .DisplayMember ) );
    DataTemplate dataTemplate = new DataTemplate();
    dataTemplate.VisualTree = textBlockEx;
    comboBox.SetValue( ComboBox.ItemTemplateProperty, dataTemplate );

    //CellEditorBinding
    CellEditorBindingExtension cellEditorBinding = new CellEditorBindingExtension();
    comboBox.SetBinding( ComboBox.SelectedValueProperty, cellEditorBinding.ProvideValue( null ) as BindingBase );

    //Fill the combo box
    List<IEntity> entityList = GetAll( aListColumnValueListAttribute.Type );
    comboBox.SetValue( ComboBox.ItemsSourceProperty, entityList );

    cellEditor.EditTemplate = new DataTemplate();
    cellEditor.EditTemplate.VisualTree = comboBox;

    aColumn.CellEditor = cellEditor;
    aColumn.CellContentTemplate = dataTemplate;

    return aColumn;
    }

    What am I doing wrong???

    Thanks again for your help.

    Imported from legacy forums. Posted by Simon (had 1561 views)

    Xceed Support
    Member
    Post count: 5658

    The reason why you CellContentTemplate is not displaying anything is because the DataTemplate you built is made to display the actual “Entity”, and not the “key” identifying the entity…

    I suggest you take a look in the Solid Foundation sample… In this sample, we built a class that create a Dictionary between the Key and the actual data item from a “relation”…

    In our sample, we based ourselves on a DataTable, but nothing would prevent your from adapting the solution to your needs…

    In the sample, you will find all the information required to build a ComboBox… However, the templates are written in XAML, you would need to perform the conversion to C# code…

    Imported from legacy forums. Posted by Marcus [Xceed] (had 9498 views)

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.