选择 WPF 控件 [英] Choosing WPF controls

查看:22
本文介绍了选择 WPF 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为用户提供更改 NLog 规则日志级别的选项.

I need to provide for users the option to change logging level of NLog rules.

有 12 条规则,每条规则都有自己的日志记录级别.

There are 12 rules and each one has it's own logging level.

您可以推荐使用哪些控件在 WPF 中提供此选项?

Which controls could you recommend to use to provide this option in WPF?

推荐答案

我不熟悉 NLog,但我想如果您必须在少量预先确定的选项之间进行选择,那么使用 ComboBox 是最好的 UI 元素.

I'm not familiar with NLog, but I guess if you must pick between a small amount of pre-determined options then a ComboBox is the best UI element for that.

你说你有 12 个日志级别,所以在这种情况下,使用 ItemsControl 来实际显示这些项目而不是自己创建所有 UI 元素是最有意义的:

You said you have 12 Log Levels, So in that case it makes most sense to use an ItemsControl to actually show these items instead of creating all the UI elements yourself:

<Window x:Class="MiscSamples.LogLevelsSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="LogLevels" Height="300" Width="300">
    <ItemsControl ItemsSource="{Binding LogRules}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Width="100" Margin="2" Text="{Binding Name}"/>
                    <ComboBox ItemsSource="{Binding DataContext.LogLevels, RelativeSource={RelativeSource AncestorType=Window}}"
                              SelectedItem="{Binding LogLevel}" Width="100" Margin="2"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

背后的代码:

    public partial class LogLevelsSample : Window
    {
        public LogLevelsSample()
        {
            InitializeComponent();

            DataContext = new LogSettingsViewModel();
        }
    }

视图模型:

    public class LogSettingsViewModel
    {
        public List<LogLevels> LogLevels { get; set; }

        public List<LogRule> LogRules { get; set; } 

        public LogSettingsViewModel()
        {
            LogLevels = Enum.GetValues(typeof (LogLevels)).OfType<LogLevels>().ToList();

            LogRules = Enumerable.Range(1, 12).Select(x => new LogRule()
                                                               {
                                                                   Name = "Log Rule " + x.ToString(),
                                                                   LogLevel = MiscSamples.LogLevels.Debug
                                                               }).ToList();
        }
    }

数据项:

    public class LogRule
    {
        public string Name { get; set; }

        public LogLevels LogLevel { get; set; }
    }

    public enum LogLevels
    {
        Trace,
        Debug,
        Warn,
        Info,
        Error,
        Fatal
    }

结果:

注意事项:

  • 我看到您有几个 WPF 未回答的问题,而且您看起来很沮丧.别这样这是一个美丽而优秀的框架.我创建了这个例子,希望你能看到它真正的美.如果没有更多的代码和努力,您就不可能在 winforms 中实现相同的示例.
  • 看看这段代码到底有多简单和漂亮.我正在大量使用 WPF 的数据绑定功能,这让一切变得更加简单.
  • 请注意,没有一行代码引用或操作任何 UI 元素.我所做的只是创建正确的数据结构,然后创建正确的 UI 来显示/操作它.
  • 数据和 UI 之间的粘合剂"是 DataContext 属性,所有 XAML 绑定都针对此属性进行解析.
  • 如果你来自 winforms 或其他传统背景,你真的需要忘记你从中知道的一切并拥抱 MVVM(点击链接,它不是维基百科).
  • 同样,我对 NLog 不熟悉,所以我不确定我创建的数据结构是否符合您的需要.无论如何,请告诉我.
  • 如果您需要进一步的帮助,请告诉我.我很乐意帮助您迈出 WPF 的第一步.
  • I see you have several unanswered WPF questions and you look quite frustrated about it. Don't be. It's a beautiful and excellent framework to work with. I created this example in the hope you can see the real beauty of it. There's no chance you can achieve this same example in winforms without a LOT more code and effort.
  • Take a look at how simple and beautiful this code actually is. I'm doing extensive use of WPF's databinding capabilities and that makes everything a LOT easier.
  • Note there's not a single line of code that references or manipulates any UI element. All I have done is to create the proper data structures and then create the proper UI to display/manipulate it.
  • The "glue" between the data and the UI is the DataContext property, which is what all XAML bindings are resolved against.
  • If you come from winforms or other traditional backgrounds, you really need to forget everything you know from that and embrace MVVM (Click on the link it's not wikipedia).
  • Again, Im not familiar with NLog so I'm not sure whether the data structure I created matches what you need. In any case let me know.
  • Let me know if you need further assistance. I'll be glad to help you in your first steps in WPF.

这篇关于选择 WPF 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆