I have a data structure to represent an option flag structure.
public class Flag { public string Name { get; set; } public bool Value { get; set; } private bool isChecked; public bool IsChecked { get { return isChecked; } set { isChecked = value; Parent.Update();} } public Flags Parent { get; set; } 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 - 1, 1); Selected = sb.ToString(); } protected override void InsertItem(int index, Flag item) { item.Parent = this; base.InsertItem(index, item); } }
Hello,
I'm sorry for late reply.
Look at the attached project. I implement the behavior that you want.
Tell me if you need more help or you have any questions.
Regards,
Anastas
This helped me with my issue thanks.
This solution works well but how would one implement this on the cell of xamDataGrid ? I need an option dropdown menu to select flag type options in the cell of a xamDataGrid.
Once selected the value of the options would be displayed in the cell as above.
I know i can use an adorner to but a combobox dropdown of all options would be far slicker.