我需要为用户提供改变的NLOG规则日志记录级别的选项。
有12条规则,每一个都有它自己的日志记录级别。
控制您会推荐使用提供WPF这个选项?
我需要为用户提供改变的NLOG规则日志记录级别的选项。
有12条规则,每一个都有它自己的日志记录级别。
控制您会推荐使用提供WPF这个选项?
我不熟悉NLOG,但我想如果你必须预先确定的选项少量之间挑那么ComboBox
是,最好的UI元素。
你说你有12个日志级别,因此,在这种情况下,它使大多数使用的感觉ItemsControl
实际显示这些项目,而不是创建所有的UI元素自己:
<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
}
结果:
需要注意的事项:
DataContext
属性,它是所有XAML绑定是针对解决。