[英]Creating RadioButtons from an enum in C#
我想用一個與枚舉值對應的RadioButton
對象填充StackPanel
。 每個按鈕的處理程序應運行任意計算,該計算采用相應的枚舉值。
這是我提出的方法:
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
用於枚舉FigureHorizontalAnchor
。 我想要每個按鈕的動作來設置一個名為figure
的特定Figure
的HorizontalAnchor
屬性。 這是我如何調用EnumToRadioButtonPanel
:
var figure = new Figure();
var stackPanel = new StackPanel();
EnumToRadioButtonPanel(stackPanel, typeof(FigureHorizontalAnchor),
val =>
{
figure.HorizontalAnchor = (FigureHorizontalAnchor)
Enum.ToObject(typeof(FigureHorizontalAnchor), val);
});
我的問題是,有沒有更好的方法來實現這一目標? 我應該使用“綁定”技術嗎? 我在SO上看到了幾個相關的問題,但是它們涉及在XAML中布置RadioButton
; 我想通過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);
}
}
}
如你所知,有更多的“WPF”方法可以做到這一點。
不要通過代碼創建控件。 對WinForms來說有點好,但是你不想在WPF中做到這一點。
如果ItemsControl
項列表中創建控件集合,請使用ItemsControl
:
<ItemsControl Source="{Binding ItemsIWantToCreateControlsFor}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton
Content="{Binding APropertyDefinedInMyItem}"
Command="{Binding ACommandDefinedInMyItemThatIsExecutedWhenPressed}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
這是EnumToRadioButtonPanel
的另一個版本,它似乎通過在類型上具有通用性而導致更清晰的代碼。
void EnumToRadioButtonPanel<T>(Panel panel, Action<T> proc)
{
Array.ForEach((int[])Enum.GetValues(typeof(T)),
val =>
{
var button = new RadioButton() { Content = Enum.GetName(typeof(T), val) };
button.Click += (s, e) => proc((T)Enum.ToObject(typeof(T),val));
panel.Children.Add(button);
});
}
在此版本中,對EnumToRadioButtonPanel
的調用如下所示:
EnumToRadioButtonPanel<FigureHorizontalAnchor>(
stackPanel,
val => figure.HorizontalAnchor = val);
代替:
EnumToRadioButtonPanel(stackPanel, typeof(FigureHorizontalAnchor),
val =>
{
figure.HorizontalAnchor = (FigureHorizontalAnchor)
Enum.ToObject(typeof(FigureHorizontalAnchor), val);
});
以下是整個示例。 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>
C#:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace EnumToRadioButtonPanel
{
public partial class MainWindow : Window
{
void EnumToRadioButtonPanel<T>(Panel panel, Action<T> proc)
{
Array.ForEach((int[])Enum.GetValues(typeof(T)),
val =>
{
var button = new RadioButton() { Content = Enum.GetName(typeof(T), val) };
button.Click += (s, e) => proc((T)Enum.ToObject(typeof(T),val));
panel.Children.Add(button);
});
}
public MainWindow()
{
InitializeComponent();
var figure = new Figure();
var stackPanel = new StackPanel();
Content = stackPanel;
EnumToRadioButtonPanel<FigureHorizontalAnchor>(
stackPanel,
val => figure.HorizontalAnchor = 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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.