Welcome to the Xceed Community Sign in | Join | Help
Community Search  

In-code CellEditor for column edit

Sort Posts: Previous Next
  •  04-26-2008, 9:03 PM Post no. 11768

    In-code CellEditor for column edit

    I'm trying to create setup a simple CellEditor for a column so that it has a combo box with the 8 directions of the compass rose (NW, N, NE, E, SE, S, SW, W), and I need to do this in code (not in XAML), but I'm having difficulty in translating the technique. The SolidFoundation example gives the following XAML code:

           <xcdg:CellEditor x:Key="employeeEditor">
             <xcdg:CellEditor.EditTemplate>
                <DataTemplate>
                   <ComboBox BorderThickness="0"
                             BorderBrush="Transparent"
                             Background="Transparent"
                             Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(TextElement.Foreground)}"
                             MaxHeight="28"
                             VerticalContentAlignment="Top"
                             SelectedValuePath="EmployeeID"
                             ItemsSource="{Binding Source={x:Static Application.Current},Path=Employees}"
                             ItemTemplate="{StaticResource employeeDataTemplate}"
                             SelectedValue="{xcdg:CellEditorBinding}">
                      <ComboBox.Resources>
                         <Style TargetType="Popup">
                            <Setter Property="TextElement.Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
                         </Style>
                      </ComboBox.Resources>
                   </ComboBox>
                </DataTemplate>
             </xcdg:CellEditor.EditTemplate>

    Where I'm getting particularly hung up is the 4th line. I'm not yet an expert at converting XAML to C#, but if you look at the overloaded constructors on the DataTemplate object, it seems to take either an object which is supposed to be a type, or a string for XML. But, in the XAML above, it seems as though the actual combo box is being passed in.

    Basically, I want to do something like this (BTW, is there a better way to post code? Tag perhaps? I tried to read the forum FAQ but it came up as HTML when rendered in both Firefox and IE, so I'm not sure how to do it.):

                ComboBox comboBox = new ComboBox();
                comboBox.Items.Add("NW");
                comboBox.Items.Add("N");
                comboBox.Items.Add("NE");
                comboBox.Items.Add("E");
                comboBox.Items.Add("SE");
                comboBox.Items.Add("S");
                comboBox.Items.Add("SW");
                comboBox.Items.Add("W");

                dataGridControl.Columns[0].CellEditor.EditTemplate.DataType = comboBox;

    But I get a run-time error that the object is not a type or a string.

    So my question is, what is the translation from XAML to C# in this instance, as it is clearly not what I think it should be.

    Also, is there a simple (as in a couple lines of code, not a complete, complex application) example of how to setup different editors (comboboxes, buttons, checkboxes, etc.) for columns? Preferably in code (XAML is only useful to a point). That would be most helpful.

    Thanks!

  •  04-27-2008, 8:18 AM Post no. 11770 in reply to 11768

    Re: In-code CellEditor for column edit

    This thread should point you in the right direction; however, keep in mind that from a WPF stand-point it is not recommended to create DataTemplates in code. In fact, the class that is used to create them ( FrameworkElementFactory ) has been obsoleted.

    Technical Writer - Xceed Software

    Of all the things I've lost, I miss my mind the most. - Mark Twain
    Filed under:
  •  04-27-2008, 12:09 PM Post no. 11771 in reply to 11770

    Re: In-code CellEditor for column edit

    Thank you for your response, Jenny. From the thread you referenced, I think I am closer, but still not quite there. This is what I currently have:

     

                List<String> itemList = new List<String>();
                itemList.Add("NW");
                itemList.Add("N");
                itemList.Add("NE");
                itemList.Add("E");
                itemList.Add("SE");
                itemList.Add("S");
                itemList.Add("SW");
                itemList.Add("W");

                Binding binding = new Binding();
                binding.Source = itemList;

                FrameworkElementFactory factory = new FrameworkElementFactory(typeof(ComboBox));
                Xceed.Wpf.DataGrid.Markup.CellEditorBindingExtension cellEditorBindingExtension = new     Xceed.Wpf.DataGrid.Markup.CellEditorBindingExtension();
                factory.SetBinding(ComboBox.ItemsSourceProperty, (Binding)cellEditorBindingExtension.ProvideValue(null)); // Casted parameter 2 to Binding

                DataTemplate editTemplate = new DataTemplate(typeof(ComboBox));
                editTemplate.VisualTree = factory;

                dataGridControl.Columns[0].CellEditor.EditTemplate = editTemplate;

     

    I have two issues:

     
    1. The code in the thread did not compile because CellEditorBindingExtension.ProvideValue() returns an object and SetBinding expects a Binding object. I casted the result to Binding. Is this safe to do?

    2. This still doesn't turn the column into a combo box column (it's still just a regular text column), presumably because I'm not actually binding anything to the data that I've created. This seems to be where the OP of the other thread left off, and he asked on it but did not respond to the response he got. That response was:
     

    Secondly... In fact, I guess what you could do is bind the ItemsSource property to any data source (as required for your needs)... The important thing would be to use the CellEditorBinding to bind the SelectedValue, SelectedIndex or SelectedItem property.

    This is the part that I am unsure of. I don't know what the ItemsSource property is referring to. The column, CellEdit, factory, CellEditorBingingExtension, and EditTemplate all do not have an ItemsSource property. I am also unsure how to use the CellEditorBingingExtension to do the binding as he says.

     

    EDIT:

    I was able to get the drop down box to appear and to allow me to select the items I wanted by passing the binding list in as the second parameter of SetBinding, but this doesn't use the CellEditorBindingExtension as Marcus suggests in the other post. Also when I switch focus to another row the selection doesn't seem to keep, the old value is reverted. So I'm still not there yet but I wanted to post that I was able to get the combo box to appear. The values just don't take.

  •  04-27-2008, 3:51 PM Post no. 11772 in reply to 11771

    Re: In-code CellEditor for column edit

    Got it:

     

                List<String> itemList = new List<String>();
                itemList.Add("NW");
                itemList.Add("N");
                itemList.Add("NE");
                itemList.Add("E");
                itemList.Add("SE");
                itemList.Add("S");
                itemList.Add("SW");
                itemList.Add("W");

                Binding binding = new Binding();
                binding.Source = itemList;

                FrameworkElementFactory factory = new FrameworkElementFactory(typeof(ComboBox));
                factory.SetValue(ComboBox.ItemsSourceProperty, binding);

                Xceed.Wpf.DataGrid.Markup.CellEditorBindingExtension cellEditorBindingExtension = new Xceed.Wpf.DataGrid.Markup.CellEditorBindingExtension();
                factory.SetValue(ComboBox.SelectedValueProperty, cellEditorBindingExtension.ProvideValue(null) as BindingBase);

                DataTemplate editTemplate = new DataTemplate(typeof(ComboBox));
               
                editTemplate.VisualTree = factory;

                CellEditor cellEditor = new CellEditor();
                cellEditor.EditTemplate = editTemplate;
                dataGridControl.Columns[0].CellEditor = cellEditor;

    This post was very helpful, as well as the one Jenny referenced above. The post I just linked pointed out that I'd need a new CellEditor or all of my editors will inherit the new combo box, as well as showed how ot use the CellEditorBindingExtension.

  •  06-12-2008, 4:05 AM Post no. 12888 in reply to 11770

    Re: In-code CellEditor for column edit

    Hi Jenny and all support team.

    There are alternatives to the use of FrameworkElementFactory ?
     
    Alessandro
  •  06-12-2008, 8:03 AM Post no. 12897 in reply to 12888

    Re: In-code CellEditor for column edit

    Through code-behind, no... But be advised that XAML is the recommended way to create any templates.



    Marc Laroche
    Software Developer
    Xceed Software Inc.


    I don’t suffer from insanity, I enjoy every minute of it. - Unknown
  •  07-07-2008, 10:18 AM Post no. 13451 in reply to 12897

    Re: In-code CellEditor for column edit

    Is it somehow possible to have a XAML EditorTemplate, e.g. myComboBoxTemplate, and then in code-behind use that template in multiple columns but for each colun change the itemssource?

     This  would be useful in so many scenarios.

     

    regards,

    Bjarke

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2008 Xceed Software Inc.