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
170
XamComboEditor with multi select checkboxes - how can I get DisplayText to show my combined selefction
posted

I have a data structure to represent an option flag structure.

    public class Flag
    {
        public string Name { getset; }
        public bool Value { getset; }
        private bool isChecked;
        public bool IsChecked
        {
            get { return isChecked; }
            set { isChecked = value; Parent.Update();}
        }
        public Flags Parent { getset; }
        public Flag() {}
    }

This will be a dynamic field on a XamGrid where each row will have different options as part of the row data item. First I am trying to get it working on its own before I use it with a xamGrid.

I am using a XamComboEditor with a data template to display checkbox with each item. 

        <igEditor:XamComboEditor IsAlwaysInEditMode="True" Name="TestFlagsCombo"
                ItemsSource="{Binding TestFlags}" DisplayMemberPath="{Binding Parent.Selected, Mode=OneWay}">
            <igEditor:XamComboEditor.Resources>
                <DataTemplate DataType="{x:Type local:Flag}">
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding Path=IsChecked}" VerticalAlignment="Center" />
                        <TextBox Margin="4" Text="{Binding Path=Name}" VerticalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </igEditor:XamComboEditor.Resources>
        </igEditor:XamComboEditor>

So far the combo box displays the items in TestFlags, which is a class derived from ObservableCollection. What I want is for the ComboBox display text to show a string indicating what options have been selected. for example "Item1|Item3|Item4"  would mean 3 items selected. One method I have tried is to give each Flag a reference to the collection "Parent",  which has a Selected Property which is updated to the string representation of all IsChecked Flags. This is all working. What I cant get is the combo DisplayText to bind to this Selected property. I have tried

 DisplayMemberPath="{Binding Parent.Selected}" as Parent is a property on Flag pointing back to the Collection to access the Selected property that I want to display.

 Is there a way I can achieve this or is there a better solution to what I'm trying to do? Here is the Flags class to complete the source.

    public class Flags : ObservableCollection<Flag>
    {
        private string selected;
        public string Selected
        {
            get { return selected; }
            set
            {
                selected = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Selected"));
            }
        }
        
        public Flags() {}

        public void Update()
        {
            StringBuilder sb = new StringBuilder();
            this.Where(x => x.IsChecked).Select(x=> x.Name).Run(s => sb.Append(s).Append("|"));
            if (sb.Length > 0)
                sb.Remove(sb.Length - 11);
            Selected = sb.ToString();
        }
        protected override void InsertItem(int index, Flag item)
        {
            item.Parent = this;
            base.InsertItem(index, item);
        }
    }