[英]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.