Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1280
XamDataGrid.Field template for multi selecting items from a list
posted

Hi,

I have struggled to create a Field template for a List<Item> in XamDataGrid. For the purpose I have tried with both ComboBoxField and TemplateField, and could not succeed with both. 

Goal:

My goal is to create a templated field (Test 2)

where a text represents the items in the list, and when in edit mode, to have a ComboBox where items are listed with CheckBoxes to select.

So far I have managed to create the field where the correct data is shown, but when in edit mode, it doesn't really work.

           //DISPLAY TEMPLATE
            var displayTxBlFactory = new FrameworkElementFactory(typeof(TextBlock));
            var displayTxBlBinding = new Binding() {Converter = new EnumListToStringConverter()};
            displayTxBlFactory.SetBinding(TextBlock.TextProperty, displayTxBlBinding);
            displayTxBlFactory.Name = "EnumDisplayTextBlock";
            var displayTxBlTemplate = new DataTemplate {VisualTree = displayTxBlFactory};

            DisplayTemplate = displayTxBlTemplate;

            //EDITING TEMPLATE
            //**CHECK BOX**
            var editCbFactory = new FrameworkElementFactory(typeof(CheckBox));
            var editCbBinding = new Binding(".") {Converter = new CheckBoxIsCheckedToEnumListConverter(), ConverterParameter = EnumSource};
            editCbFactory.SetBinding(ToggleButton.IsCheckedProperty, editCbBinding);
            var editCbTemplate = new DataTemplate {VisualTree = editCbFactory};
            //**COMBO BOX**
            var editCmbFactory = new FrameworkElementFactory(typeof(ComboBox));
            editCmbFactory.SetValue(ItemsControl.ItemsSourceProperty, ItemsSource);
            editCmbFactory.SetValue(ItemsControl.ItemTemplateProperty, editCbTemplate);
            var editCmbTemplate = new DataTemplate() {VisualTree = editCmbFactory};

            EditTemplate = editCmbTemplate;

The above code is used to create the DisplayTemplate and EditTemplate of the TemplateField.

My struggle is how to provide the bound data to the checkboxes in the template.

 public class CisItem : INotifyPropertyChanged
    {
        public CisItem(int id, string name, int enumId, List<CisItemEnum> enums)
        {
            _itemId = id;
            _itemName = name;
            _enumId = enumId;
            _cisItemEnums = enums;
        }

        public string ItemName {get ; set;}          
        public int ItemId {get ; set;}     
        public int EnumId {get ; set;}     
        public CisItemEnum CisItemEnum {get ; set;}     
        public string SelectedEnums {get ; set;}     
        public List<CisItemEnum> SelectedEnums1 {get ; set;}     
        public List<CisItemEnum> SelectedEnums2 {get ; set;}     
        public List<CisItemEnum> CisItemEnums => _cisItemEnums;
   }

Above is the structure of the item that is used to bind to the grid.

The TemplateField is bound to the SelectedEnums2 property.
the property is of type List<CisItemEnum>.

Note: I am not interested in the Add and Remove methods of List or ObservableCollection.
I need to call the setter of the property.

Question:

How can I make a template for such a field that it is either by checking/unchecking the checkboxes or when field editing has ended it calls the setter of the property with a new list ?

Sample:

3652.WpfAllPurposesTest.rar