I set up the following example, which demonstrates the binding using a value converter.
Here is the Window1.xaml.cs:
private ObservableCollection<ByteData> _collection = new ObservableCollection<ByteData>();
public Window1()
{
InitializeComponent();
_collection.Add(new ByteData(new byte[] { 12, 54 }));
_collection.Add(new ByteData(new byte[] { 1, 2, 3, 4, 5 }));
_collection.Add(new ByteData(new byte[] { 15 }));
}
public ObservableCollection<ByteData> ObservableCollection
{
get { return _collection; }
}
public class ByteData
{
byte[] _data;
public ByteData(byte[] data)
{
_data = data;
}
public byte[] Data
{
get { return _data; }
set { _data = value; }
}
}
Here is the Window1.xaml:
<Window x:Class="TestWpfApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpfApplication"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="300" Width="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<local:ByteToStringConverter x:Key="byteToStringConverter"/>
</Window.Resources>
<StackPanel>
<ListView ItemsSource="{Binding ObservableCollection}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBox Width="200" Text="{Binding Path=Data, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged, Converter={StaticResource byteToStringConverter}}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
And here is the value converter:
public class ByteToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
byte[] data = (byte[])value;
StringBuilder byteString = new StringBuilder();
int idx;
for (idx = 0; idx < data.Length - 1; idx++)
{
byteString.AppendFormat("{0}, ", data[idx]);
}
byteString.Append(data[idx]);
return byteString.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// If you want your byte-data to be editable in the textboxes,
// this will need to be implemented.
return null;
}
}