Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF ComboBox with CheckBoxes bound to flags enumeration TwoWay
    primarykey
    data
    text
    <p>I am in the middle of creating WPF utility library but I'm stuck on how to create an easy to use enumeration converter which will help in binding any enumeration to a ComboBox control and when enumeration is declared with [Flags] attribute, ComboBox will contain checkboxes data bound to it. Solution needs to be generic so that it will be easy to use for any enumeration and I suppose it would need fresh pair of eyes of an experienced WPF developer.</p> <p>So far I've come up with a combination of ObjectDataProvider as a source of enum items and 2 converters, one for ItemsSource and another for SelectedValue. I used CollectionView as a base class for ItemsSource to use CurrentChanged but it does not work as I tried to raise OnCurrentChanged after the collection has been notified about any CheckBox being checked without converter's ConvertBack being executed.</p> <p>The problem is that data bound property to which ComboBox SelectedValue property is bound, is not updated unless I'll change the selected item (not tick the checkbox). I've set IsSynchronizedWithCurrentItem but it did not help.</p> <p>How to force updating of SelectedValue from data bound object? I have a freedom of choice so the solution might involve custom attached properties, inheriting from ComboBox etc. Being really new to WPF I think although the solution is almost right as it almost works, there can be a simpler way of getting to it.</p> <p>Another issue would be how to customize text displayed on ComboBox so it will contain aggregated selections of ticked checkboxes.</p> <p>I uploaded VS2012 project <a href="http://www.opamo.com/CheckComboTest.7z" rel="nofollow">here</a>. The structure of a project resembles real application with resources held separately (for simplicity in App.xaml).</p> <p>Sorry I have not included any code but there is a lot of code involved.</p> <p>Below is the XAML declaration for a CheckBox bound to standard enumeration (although enumeration used is with [Flags]), which works fine and further a CheckBox bound to [Flags] enumeration.</p> <pre><code>&lt;ComboBox ItemsSource="{Binding Source={StaticResource someItemsSource}}" SelectedValue="{Binding BindToSomeItems}" Style="{StaticResource enumComboBox}"/&gt; &lt;ComboBox ItemsSource="{Binding BindToSomeItems, Converter={local:LocalizedFlagsEnumConverter}}" IsSynchronizedWithCurrentItem="True" SelectedValue="{Binding BindToSomeItems, Mode=TwoWay, Converter={local:SelectedFlagsEnumConverter}}" Style="{StaticResource flagsComboBox}" /&gt; </code></pre> <p>And resources (which explain usage of value converters):</p> <pre><code>&lt;Application.Resources&gt; &lt;Style x:Key="enumComboBox" TargetType="{x:Type ComboBox}"&gt; &lt;Setter Property="ItemTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Path=.,Mode=OneWay, Converter={local:LocalizedEnumConverter}}" Style="{x:Null}"/&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;Style x:Key="flagsComboBox" TargetType="{x:Type ComboBox}"&gt; &lt;Setter Property="ItemTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;CheckBox IsChecked="{Binding Path=Checked,Mode=TwoWay}"/&gt; &lt;TextBlock Text="{Binding Path=Name,Mode=OneWay}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Application.Resources&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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