Значение WPF пусто в ValueConverter - PullRequest
0 голосов
/ 23 января 2019

У меня есть приложение WPF для отображения списка LogMessage. Класс LogMessage выглядит следующим образом:

public class LogMessage
{
    public DateTime Timestamp { get; set; }
    public LogLevel LogLevel { get; set; }
    public string Message { get; set; }
}

Я хочу привязать значение LogLevel к Foreground свойству TextBlock с помощью этого ValueConverter:

[ValueConversion(typeof(LogLevel), typeof(Brush))]
public class LogLevelToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var logLevel = (LogLevel)value;
        switch (logLevel)
        {
            case LogLevel.Info:
                return new SolidColorBrush(Colors.DarkGray);
            case LogLevel.Important:
                return new SolidColorBrush(Colors.Green);
            case LogLevel.Warning:
                return new SolidColorBrush(Colors.Orange);
            case LogLevel.Error:
                return new SolidColorBrush(Colors.Red);
            default:
                return new SolidColorBrush(Colors.DarkGray);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Вид выглядит так:

<Window x:Class="TouchLogic.EcrServer.Wpf.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TouchLogic.EcrServer.Wpf.ViewModels"
        mc:Ignorable="d"
        Title="MainView" Height="450" Width="800">

    <Window.Resources>
        <local:LogLevelToColorConverter x:Key="LogLevelToColorConverter"/>
    </Window.Resources>

    <Grid Margin="8">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="140" />
        </Grid.ColumnDefinitions>

        <ListBox Grid.Row="0" Grid.Column="0" ItemsSource="{Binding LogMessages}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Timestamp}" Foreground="{Binding LogLevel, Converter={StaticResource LogLevelToColorConverter}}"/>
                        <TextBlock Text="{Binding Message}" Foreground="{Binding LogLevel, Converter={StaticResource LogLevelToColorConverter}}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

Моя проблема в том, что value в моем преобразователе значений всегда имеет значение 0, и я не знаю почему.

...