0

我正在使用以下查询:

string query = @"SELECT r.id, user_name, user_phone, date_create, REPLACE( date_payment,  '0000-00-00 00:00:00',  'Не оплачено' ) as 
                date_payment, payment_method, amount, rs.name_ru
                FROM request AS r, request_status AS rs
                WHERE r.status = rs.id";

我正在通过以下方式绑定数据模板:

DataTemplate>
    <TextBlock VerticalAlignment="Center" Text="{Binding date_payment}" Width="135" />
</DataTemplate>

除了“ date_payment ”之外,它的抛出正确输出它的输出值来自“ System.Byte [] ”。请帮忙!!!

来自 MBen 的感谢

class ByteArrayToString : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                var listOfBytes = value as Byte[];
                string output = "";
                output = System.Text.Encoding.UTF8.GetString(listOfBytes);
                return output;
            }

            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }
4

2 回答 2

2

date_payment 是一个数组,并且您没有为 WPF 提供任何显示它的方式,因此它调用ToString. 你可以为它提供一个数据转换器。

将资源添加到您的窗口或页面:

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

在你TextBlock的这样使用它:

<TextBlock VerticalAlignment="Center" Text="{Binding date_payement, Converter={StaticResource ByteArrayConverter}}" Width="135" />

现在您需要添加一个进行转换的新类:

    class ByteArrayToString : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                var listOfBytes = value as Byte[];
                string output ="";
                output = listOfBytes.Aggregate(output, (current, elemt) => current + elemt.ToString());
                return output;
            }

            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }
于 2012-07-16T09:06:21.977 回答
0

这件事发生在我身上。这似乎是由于连接器中的错误造成的。尝试将日期列转换为 CHAR。它对我有用。

于 2012-07-16T09:04:54.830 回答