【问题标题】:bind enum to Combobox On CUSTOM CONTROL将枚举绑定到 CUSTOM CONTROL 上的 Combobox
【发布时间】:2023-03-03 00:44:01
【问题描述】:

我在 WPF 中有一个自定义控件,我需要将其上的组合框绑定到我编写的枚举,

在网上搜索我发现这是要走的路:

<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

的地方

感谢您的建议:)

【问题讨论】:

  • 使用 区域来定义它。

标签: c# wpf data-binding custom-controls


【解决方案1】:

您可以按照 cmets 中的建议使用资源。

完整的 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>

【讨论】:

  • 只是添加到@Mat 答案中,App: 是您的公共枚举所在的命名空间。
猜你喜欢
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-15
  • 2015-07-03
  • 2016-03-25
  • 2010-10-29
相关资源
最近更新 更多