Home Forums WPF controls Xceed DataGrid for WPF In-code CellEditor for column edit

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #26971 |

    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!

    Imported from legacy forums. Posted by Mike Pateras (had 1634 views)

    Xceed Support
    Member
    Post count: 5658

    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.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 1146 views)

    User (Old forums)
    Member
    Post count: 23064

    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.

    Imported from legacy forums. Posted by Mike Pateras (had 811 views)

    User (Old forums)
    Member
    Post count: 23064

    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.

    Imported from legacy forums. Posted by Mike Pateras (had 3719 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Jenny and all support team.

    There are alternatives to the use of FrameworkElementFactory ?
     
    Alessandro

    Imported from legacy forums. Posted by Alessandro (had 619 views)

    Xceed Support
    Member
    Post count: 5658

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

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

    User (Old forums)
    Member
    Post count: 23064

    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

    Imported from legacy forums. Posted by Bjarke (had 585 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi

    I added a combobox column  to xceed datagrid for wpf .

    my combobox has 2 olumns “id and name”

    my value field is ID and despley member is Name,

     

    noe I wnt to red the selected id and name from the combo

    can anyone help me?

    Imported from legacy forums. Posted by ali (had 541 views)

    User (Old forums)
    Member
    Post count: 23064

    I would put up a new post on this, as it’s considered bad forum etiquette to dig up old posts like this one.

    However, you might be able to do something like this:

    FrameworkElementFactory factory = new FrameworkElementFactory(typeof(ComboBox));

    factory.AddHandler(ComboBox.SelectionChangedEvent, new RoutedEventHandler(myHandler));

    where myHandler is the code to handle the selection change. I hope that helps!

    Imported from legacy forums. Posted by Mike Pateras (had 1694 views)

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