Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could use a value converter. Here's a very specific implementation for the target Enum, but would not be hard to see how to make the converter more generic:</p> <pre><code>[Flags] public enum Department { None = 0, A = 1, B = 2, C = 4, D = 8 } public partial class Window1 : Window { public Window1() { InitializeComponent(); this.DepartmentsPanel.DataContext = new DataObject { Department = Department.A | Department.C }; } } public class DataObject { public DataObject() { } public Department Department { get; set; } } public class DepartmentValueConverter : IValueConverter { private Department target; public DepartmentValueConverter() { } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Department mask = (Department)parameter; this.target = (Department)value; return ((mask &amp; this.target) != 0); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { this.target ^= (Department)parameter; return this.target; } } </code></pre> <p>And then use the converter in the XAML:</p> <pre><code>&lt;Window.Resources&gt; &lt;l:DepartmentValueConverter x:Key="DeptConverter" /&gt; &lt;/Window.Resources&gt; &lt;StackPanel x:Name="DepartmentsPanel"&gt; &lt;CheckBox Content="A" IsChecked="{Binding Path=Department, Converter={StaticResource DeptConverter}, ConverterParameter={x:Static l:Department.A}}"/&gt; &lt;!-- more --&gt; &lt;/StackPanel&gt; </code></pre> <p><strong>EDIT:</strong> I don't have enough "rep" (yet!) to comment below so I have to update my own post :(</p> <p>In the last comment demwiz.myopenid.com says "but when it comes to two-way binding the ConvertBack falls apart", well I've updated my sample code above to handle the ConvertBack scenario; I've also posted a sample working application <a href="http://www.box.net/shared/8xp0qqsc47" rel="noreferrer"><strong><em>here</em></strong></a> (<strong>edit:</strong> note that the sample code download also includes a generic version of the converter).</p> <p>Personally I think this is a lot simpler, I hope this helps.</p>
 

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