C#WPF显示来自位标志枚举值的复选框并获取选中的值 [英] C# WPF show checkboxes from bit flags enum values and get checked values
本文介绍了C#WPF显示来自位标志枚举值的复选框并获取选中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有点旗帜
I have a bit flags
[Flags]
public enum WeekDayName
{
sunday=1,
monday=2,
tuesday=4,
wednesday=8,
thursday=16,
friday=32,
saturday=64
}
i想要动态显示这个枚举并在WPF中获取检查值(MVVM)
i want show this enums dynamically and get checked values in WPF (MVVM)
CheckBox chb;
foreach (WeekDayName day in Enum.GetValues(typeof(WeekDayName)))
{
chb = new CheckBox();
chb.Content = day;
// chb.Name = day;
WeekDaysList.Items.Add(chb);
}
Thanx Jamshed
Thanx Jamshed
推荐答案
我将整篇文章专门用于枚举问题:枚举类型不会枚举!解决.NET和语言限制 [ ^ ]。
顺便说一下,您还可以在UI中显示其他名称或将其全球化以进一步本地化;下一篇文章专门讨论这些问题:人类可读的枚举元数据 [ ^ ]。
在简单的情况下,您可以使用第一篇文章中描述的简单技术背景:枚举类之前的生活。
-SA
I devoted the whole article to the enumeration problems: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].
By the way, you can also show alternative names in your UI or globalize them for further localization; the next article is devoted to that problems: Human-readable Enumeration Meta-data[^].
In simple cases, you can use simple techniques described in first article under the second section "Background: Life before Enumeration Class".
—SA
I found what i need
In WPF Form I have 2 elements:
Listbox: Name list,
Button
<pre lang="c#">`[Flags]
public enum WD
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}`
i将所有枚举类型添加到列表项目
i add all enum types to list itemsouce
`private void Button_Click(object sender, RoutedEventArgs e)
{
list.ItemsSource = Enum.GetValues(typeof(WD));
}`
代码检查复选框ckecked
code for check wich checkbox ckecked
`int intValue;
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
var checkBox = (CheckBox)sender;
var dc =checkBox.DataContext;
var actualValue = (WD)Enum.Parse(typeof(WD), dc.ToString());
intValue += (int)actualValue;
}`
XAML代码
XAML code
`<Button Content="Button" HorizontalAlignment="Left" Height="42" Margin="418,267,0,0" VerticalAlignment="Top" Width="79" Click="Button_Click"/>
<ItemsControl Name="list" Margin="0,0,196,0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}" Margin="2" Checked="CheckBox_Checked" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>`
Thanx Jamaxack
Thanx Jamaxack
这篇关于C#WPF显示来自位标志枚举值的复选框并获取选中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文