首页 > 解决方案 > c# Wpf 将枚举绑定到组合框

问题描述

我在将 WeekDay 枚举绑定到组合框时遇到问题。营养计划绑定效果很好。

这是我的 .xaml 代码:

<Page x:Class="WpfApp.Views.AddDailyNutritionPlan"
  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:local="clr-namespace:WpfApp.Views"
  mc:Ignorable="d" 
  d:DesignHeight="450" d:DesignWidth="800"
    Title="AddDailyNutritionPlan">
<Grid Background="#A4EE6A" >
    <ComboBox Name="cmbnutritionplan" ItemsSource="{Binding NutritionPlans}" 
              HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="119,36,0,0">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding NutritionPlanName}" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    <ComboBox Name="cmbweekday" ItemsSource="{Binding Path=WeekDays}" HorizontalAlignment="Left" 
              VerticalAlignment="Top" Width="120" Margin="119,72,0,0" RenderTransformOrigin="1.701,0.613">

    </ComboBox>
</Grid>

标签: c#wpfenumscomboboxbinding

解决方案


我现在没有具体细节,但我所做的是这样的。

  enum WeekDay { Mon, Tues };
  Dictionary<string, WeekDay> dic = new Dictionary<string, WeekDay>();
  foreach (var v in WeekDay.Enumerate)  //this is not the correct syntax 
  {
      dic.Add(v.ToString(), v);
  }

绑定字典;


推荐阅读