WPF datagrid没有正确获取十六进制值,它会显示消息Byte[]
十六进制值数组?
我是否需要更改编码中的任何内容?
答案 0 :(得分:0)
以下是使用转换器的方法:
MainWindow.xaml
<Window x:Class="ByteArrayHex.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ByteArrayHex"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel.Resources>
<local:ByteArrayToHexConverter x:Key="ByteArrayToHexConverter" />
</StackPanel.Resources>
<TextBlock Margin="5" Text="{Binding ba, Converter={StaticResource ByteArrayToHexConverter}}" Background="Beige" />
</StackPanel>
</Window>
MainWindow.xaml.cs
using System;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Windows;
using System.Windows.Data;
namespace ByteArrayHex
{
public partial class MainWindow : Window
{
public byte[] ba { get; set; }
public MainWindow()
{
InitializeComponent();
this.ba = new byte[] { 0x49, 0x4A, 0x4B, 0x4C, 0x4D };
this.DataContext = this;
}
}
public class ByteArrayToHexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
byte[] ba = value as byte[];
if (ba == null)
return null;
SoapHexBinary shb = new SoapHexBinary(ba);
return shb.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}