Sunday, 15 April 2012

c# - ComboBox for enumeration System.IO.Ports.Parity in WPF -



c# - ComboBox for enumeration System.IO.Ports.Parity in WPF -

i want (in c#) populate list of admissible values combobox admissible values of enumeration system.io.ports.parity. end created collection:

public class theparitysource : observablecollection<parity> { public theparitysource() { array parities = system.enum.getvalues( typeof( parity ) ); foreach (parity p in parities) this.add(p); } }

(btw: there oneliner initialization?) , made datacontext of combobox:

... xmlns:local="clr-namespace:mynamespace" ... <combobox ...> <combobox.datacontext> <local:theparitysource /> </combobox.datacontext> </combobox>

the combobox, however, remains empty (it shown empty, seems have right length), though can see in debugger how theparitysource gets populated. approach work in combobox (even in same class) baudrate. initialize integer values, guess somehow related fact i'm using enum here, i'm clueluess might reason. pointers? need write converter?

(of course of study can work around creating list of strings enum, kind of unpleasant...)

edit: i'd prefer of in xaml. there simple way that?

you can in xaml using objectdataprovider

in window.resources (or whatever resources using) setup objectdataprovider.

to setup objectdataprovider enums set objecttype {x:type sys:enum} , methodname getvalues fill combobox actual enums or can utilize getnames fill combobox string representaion of enum

xmlns:sys="clr-namespace:system;assembly=mscorlib" xmlns:io="clr-namespace:system.io.ports;assembly=system" <window.resources> <objectdataprovider methodname="getvalues" objecttype="{x:type sys:enum}" x:key="parityvalues"> <objectdataprovider.methodparameters> <x:type typename="io:parity" /> </objectdataprovider.methodparameters> </objectdataprovider> </window.resources>

then bind combobox

<combobox itemssource="{binding source={staticresource parityvalues}}" />

result:

c# .net wpf combobox

No comments:

Post a Comment