Exclude enum value on binding.
-
Wednesday, September 24, 2008 8:18 AM
Can I somehow exclude an enum value from taking part in binding to control ?
Say I have :enum TEST{ Undefined, Top, Bottom }
I would like to exclude Undefined when binding . Can I do this ?
All Replies
-
Wednesday, September 24, 2008 9:38 AM
<Page x:Class="WPFTest.Page1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Page1"> <StackPanel x:Name="stack"> <ComboBox x:Name="combo"/> </StackPanel> </Page> using System; using System.Collections.Generic; using System.Windows; using System.Linq; using System.Windows.Controls; namespace WPFTest { /// <summary> /// Interaction logic for Page1.xaml /// </summary> public partial class Page1 : Page { public Page1() { InitializeComponent(); string[] TestNames = Enum.GetNames(typeof(TEST)); var list = from test in TestNames where test != "Undefined" select test; combo.ItemsSource = list; } enum TEST { Undefined, Top, Bottom } } }
Bigsby, Lisboa, Portugal- Marked As Answer by sirrocco Wednesday, September 24, 2008 10:35 AM
-
Wednesday, September 24, 2008 9:56 AMHi,
There are quite a few different ways which you can bind an enum to a control - if you let us know what technique you are using we could suggest an appropriate solution.
Colin E.
-
Wednesday, September 24, 2008 9:57 AMEdit:
There's a problem with the approach given by Bigsby : When I'm binding the Combo's SelectedValueProperty to a another object that has a property of type TEST (the enum) . The SelectedValue of the combo is null - and that because it's datasourse is a list of strings not the enum itself.
Any suggestions ?
-----
I was using an ObjectDataProvider.
However, if there is another way - I'll be happy to hear about it :). -
Wednesday, September 24, 2008 10:19 AM
This :from test in TestNames where test != "Undefined" select Enum.Parse(typeof(Test),test)
Worked like a charm :D. Thanks everybody.
PS: @Colin - if there's another way, please post it - thanks.
- Marked As Answer by sirrocco Wednesday, September 24, 2008 10:35 AM

