[英]bind enum to Combobox On CUSTOM CONTROL
我在WPF中有一个自定义控件,我需要将一个comboBox绑定到我写的枚举上,
在网上搜索我发现这是要走的路:
<ObjectDataProvider
MethodName="GetDict"
ObjectType="{x:Type App:EnumDescriptionValueDict}"
x:Key="EnumDescriptionDict">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="App:Transmission"></x:Type>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ComboBox
ItemsSource="{Binding Source={StaticResource EnumDescriptionDict}}"
DisplayMemberPath="Key"
SelectedValuePath="Value"/>
但我的控制XAML
<UserControl x:Class="WpfControlFoo.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Width="799" Height="107">
所以我找不到插入ObjectDataProvider XAML的地方
谢谢你的建议:)
您可以按照注释中的建议使用资源。
完整的XAML:
<UserControl x:Class="WpfControlFoo.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:App="myNamespaceWhereTheEnumIsLocated"
mc:Ignorable="d" Width="799" Height="107">
<UserControl.Resources>
<ObjectDataProvider
MethodName="GetDict"
ObjectType="{x:Type App:EnumDescriptionValueDict}"
x:Key="EnumDescriptionDict">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="App:Transmission"></x:Type>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>
<ComboBox
ItemsSource="{Binding Source={StaticResource EnumDescriptionDict}}"
DisplayMemberPath="Key"
SelectedValuePath="Value"/>
</UserControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.