wpf binding combobox to enum in different namespace

三世轮回 提交于 2020-01-01 15:06:23

问题


I'm trying to bind a XAML ComboBox so that its list items are the members of the System.IO.Ports.Parity enum.

I've found plenty of examples of databinding enums, but it seems these don't work if the enum is in a different namespace (like System.IO.Ports).

Right now I have:

<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="parityValues">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="System.IO.Ports.Parity" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

But I get the error "Type reference cannot find public type named 'System.IO.Ports.Parity'."

Any ideas how to accomplish this?


回答1:


Adding something like

xmlns:sysioports="clr-namespace:System.IO.Ports;assembly=System"

and then changing the <x:Type /> line to

<x:Type TypeName="sysioports:Parity" />

Should make it work.




回答2:


just because you should include and use namespace like this

<Window xmlns:custom="clr-namespace:System.IO.Ports.Parity;assembly=SampleLibrary">
...
  <x:Type TypeName="custom:Parity" />
...
</Window>


来源:https://stackoverflow.com/questions/1138573/wpf-binding-combobox-to-enum-in-different-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!