c# - 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>
解决方案
我现在没有具体细节,但我所做的是这样的。
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);
}
绑定字典;
推荐阅读
- performance - 允许使 SQL 中的 MERGE 操作更快地工作的方法
- python - pandas 通过比较其他字段和字典值来计算字段
- ssh - Cygwin 自行消失并弄乱了我的下载文件夹权限(我认为)
- ios - 我们可以从firebase获取pdf或excel的记录吗
- python - pip 如何在不同的环境中管理相同的包以提高空间效率
- python - 从源到目的地的矩阵中的广度优先搜索
- javascript - 如何为未定义的窗口对象编写测试用例 | Vue 测试工具 | 笑话框架
- git - 自动计算整个 git 分支上的修改(例如添加/删除的字母)
- html - 如何使img大小受单元格大小限制
- laravel - 如果条件在控制器中不起作用,则在 laravel 中