Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>A different solution</strong></p> <p>I use a very different solution for this situation that I think is much cleaner. Using a couple of utility classes I created I can bind the SelectedOptions directly without having to write application code to deal with commands, collection updates, etc.</p> <p><strong>EnumExpansion class</strong></p> <p>I created a simple class with the following signature:</p> <pre><code>public class EnumExpansion : DependencyObject, IList, INotifyCollectionChanged { public object EnumValue { ... // DependencyProperty ... // IList &amp; INotifyCollectionChanged implementation } </code></pre> <p>EnumValue can be set to any enum type. When EnumValue is set, the internal ObservableCollection is updated by removing all flags not in the current EnumValue and adding all flags in the current EnumValue. Whenever the internal collection is changed, EnumValue is updated.</p> <p><strong>BindableSelectedItems property</strong></p> <p>I also created a simple attached property that allows ListBox to bind its SelectedItems property. It is used like this:</p> <pre><code>&lt;ListBox ItemsSource="{Binding Options}" edf:ListBoxHelper.BindableSelectedItems="{Binding SelectedOptionsExpansion}" /&gt; </code></pre> <p>The attached property is implemented by subscribing to SelectionChanged on the ListBox and CollectionChanged on the property value (which is of type INotifyCollectionChanged).</p> <p><strong>Initializing SelectedOptionsExpansion</strong></p> <p>You can do this in XAML but it is quite easy in code:</p> <pre><code>public EnumExpansion SelectedOptionsExpansion { get; set; } ... SelectedOptionsExpansion = new EnumExpansion(); BindingOperations.SetBinding(SelectedOptionsExpansion, EnumExpansion.EnumValueProperty, new Binding { Path = "SelectedOptions", Source = this }); ... </code></pre> <p><strong>How it works</strong></p> <p>Enum to ListBox:</p> <ol> <li>SelectedOptions changes, either via code or data binding</li> <li>The SelectedOptionsExpansion's EnumValue property is updated by the binding, which causes the EnumExpansion's collection to change.</li> <li>The CollectionChange event is picked up by the ListBoxHelper code which updates the selection in the ListBox.</li> </ol> <p>ListBox to Enum:</p> <ol> <li>An item is selected or deselect in the ListBox</li> <li>ListBoxHelper picks it up and updates the EnumExpansion collection, which causes the EnumValue property to update.</li> <li>Since EnumValue is BindsTwoWayByDefault, the SelectedOptions value is updated.</li> </ol> <p><strong>Why I prefer this solution</strong></p> <p>Once the two utility classes are created, the rest of the binding process is straightforward data binding. There is no need to handle commands or update collections in your application code - it is all hidden within the utility classes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload