我想用与枚举值对应的对象填充StackPanel
a RadioButton
。每个按钮的处理程序都应该运行一个任意计算,该计算采用相应的枚举值。
这是我想出的方法:
void EnumToRadioButtonPanel(Panel panel, Type type, Action<int> proc)
{
Array.ForEach((int[])Enum.GetValues(type),
val =>
{
var button = new RadioButton() { Content = Enum.GetName(type, val) };
button.Click += (s, e) => proc(val);
panel.Children.Add(button);
});
}
例如,假设我想要RadioButton
s 作为 enum FigureHorizontalAnchor
。我希望每个按钮的操作来设置HorizontalAnchor
特定Figure
调用的属性figure
。这是我调用的方式EnumToRadioButtonPanel
:
var figure = new Figure();
var stackPanel = new StackPanel();
EnumToRadioButtonPanel(stackPanel, typeof(FigureHorizontalAnchor),
val =>
{
figure.HorizontalAnchor = (FigureHorizontalAnchor)
Enum.ToObject(typeof(FigureHorizontalAnchor), val);
});
我的问题是,有没有更好的方法来做到这一点?我应该改用“绑定”技术吗?RadioButton
我在这里看到了一些关于 SO 的相关问题,但它们涉及在 XAML 中布置s ;我想通过 C# 中的代码来做到这一点。
这是上述内容的完整可运行演示。XAML:
<Window x:Class="EnumToRadioButtonPanel.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
</Window>
后面的代码:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace EnumToRadioButtonPanel
{
public partial class MainWindow : Window
{
void EnumToRadioButtonPanel(Panel panel, Type type, Action<int> proc)
{
Array.ForEach((int[])Enum.GetValues(type),
val =>
{
var button = new RadioButton() { Content = Enum.GetName(type, val) };
button.Click += (s, e) => proc(val);
panel.Children.Add(button);
});
}
public MainWindow()
{
InitializeComponent();
var figure = new Figure();
var stackPanel = new StackPanel();
Content = stackPanel;
EnumToRadioButtonPanel(stackPanel, typeof(FigureHorizontalAnchor),
val =>
{
figure.HorizontalAnchor = (FigureHorizontalAnchor)
Enum.ToObject(typeof(FigureHorizontalAnchor), val);
});
var label = new Label();
stackPanel.Children.Add(label);
var button = new Button() { Content = "Display" };
button.Click += (s, e) => label.Content = figure.HorizontalAnchor;
stackPanel.Children.Add(button);
}
}
}