我想用一个与枚举值对应的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的特定FigureHorizontalAnchor属性。 这是我如何调用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);
        }
    }
}

===============>>#1 票数:3

如你所知,有更多的“WPF”方法可以做到这一点。

不要通过代码创建控件。 WinForms来说有点好,但是你不想在WPF中做到这一点。

如果ItemsControl项列表中创建控件集合,请使用ItemsControl

<ItemsControl Source="{Binding ItemsIWantToCreateControlsFor}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <RadioButton
        Content="{Binding APropertyDefinedInMyItem}"
        Command="{Binding ACommandDefinedInMyItemThatIsExecutedWhenPressed}"/>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

===============>>#2 票数:0

这是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);
        }
    }
}

  ask by dharmatech translate from so

未解决问题?本站智能推荐: