How to exclude some enum members in the xaml when bound to a ComboBox?
-
9. května 2012 17:26
I am using VS 2010, .Net 4.0 and C#. I want to be able to exclude certain members of an enumeration when binding the enumeration to a ComboBox control. I seem to remember seeing a solution to this type of feature in a posting awhile back, but I can no longer find it.
I have an enumeration like this:
public enum MyEnum { First, Second, Third, Fourth, Fifth }I want to bind the enum to a ComboBox like this:
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="MyEnumSelection"> <ObjectDataProvider.MethodParameters> <x:Type TypeName="common:MyEnum"/> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <ComboBox IsSynchronizedWithCurrentItem="True" SelectedValue="{Binding Path=MyEnumProperty}" ItemsSource="{Binding Source={StaticResource MyEnumSelection}}"/>I seem to remember it was possible to exclude certain members from the enum by adding some xaml syntax in the ItemsSource binding code. I think it looked something like this:
<ComboBox IsSynchronizedWithCurrentItem="True" SelectedValue="{Binding Path=MyEnumProperty}" ItemsSource="{Binding Source={StaticResource MyEnumSelection}, Exclude="Fifth"}"/>Does anybody know the correct syntax to do this? Are there limitations to what can be excluded?
Thanks, Dave
Dave R&D SW Engineer Agilent Technologies
Všechny reakce
-
9. května 2012 17:58
Here is one link:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/53674ea3-34ae-4473-a655-24c52f7d0a75
Hope this helps
Remember Google is your friend when needing help
Lloyd Sheen
-
9. května 2012 18:01Moderátor
Here is one link:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/53674ea3-34ae-4473-a655-24c52f7d0a75
Hope this helps
Remember Google is your friend when needing help
Lloyd Sheen
The OP seems to want a XAML-only solution. -
9. května 2012 18:22
Lloyd,
Thanks for your reply. I also saw this link before submitting my question. Note that I am looking for a solution that only requires additional xaml. So I don't consider the appraoch at that link to be a solution/answer to my question.
Dave
Dave R&D SW Engineer Agilent Technologies
-
9. května 2012 18:34
I know you only want a xaml approach, but I suspect you will have to provide some sort of filter class to be able to accomplish what you want.
See Min Zhu's approach in this thread...
http://social.msdn.microsoft.com/Forums/en/wpf/thread/cc01796e-76a5-4bba-8241-416732f76a3c
~Christine
-
11. května 2012 6:16Moderátor
Hi DaveTMG,
I think you could try to create your own MarkupExtension to achieve this goal, there is a sample you could refer to:
http://agsmith.wordpress.com/2008/09/19/accessing-enum-members-in-xaml/
Best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Označen jako odpověď Sheldon _XiaoModerator 25. května 2012 4:38
- Zrušeno označení jako odpověď DaveTMG 25. května 2012 15:33
-
25. května 2012 15:36
Sheldon,
Not clear on why this post was marked as answered. A solution for the stated issue has not been provided. I need a way to restrict which enum members are displayed, not how to display enum members as pretty strings.
Regards, Dave
Dave R&D SW Engineer Agilent Technologies
-
29. května 2012 7:43Moderátor
Hi DaveTMG,
I think there is no build in method we could set in xaml likes "Exclude="Fifth", you could try to create your own MarkupExtension, and then you could set it as exclude=fifth.
best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Navržen jako odpověď chriga 21. prosince 2012 23:19