How to get a WPF list view for formatting a byte array as a comma delimited string?

I am trying to associate some data with a WPF list. One of the properties of my data type is of type byte[], and I would like it to be displayed as a comma-delimited string, therefore, for example, it { 12, 54 }will be displayed as 12, 54, not how Byte[] Array. I think I want to create a custom one DataTemplate, but I'm not sure. Is this the best way? If so, how do I do this? If not, what is the best way?

EDIT: I want to use this for only one column - the other properties are displayed as they are.

+3
source share
3 answers

I would suggest using ValueConverter:

[ValueConversion(typeof(byte []), typeof(string))]
public class ByteArrayConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        byte [] bytes = (byte [])value;
        StringBuilder sb = new StringBuilder(100);
        for (int x = 0; x<bytes.Length; x++)
        {
            sb.Append(bytes[x].ToString()).Append(" ");
        }
        return sb.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    #endregion
}

In your xaml, you would add it to your binding, for example:

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

...

"{Binding ByteArrayProperty, Converter={StaticResource byteArrayConverter}}"
+11
source

I would create ValueConverterone that creates a comma delimited string. You can then directly link to byte[], but specify the converter in the binding.

(I do not need to put code when Mark's answer is good).

+2
source

I installed the following example demonstrating binding using a value converter.

Here is the Window1.xaml.cs window:

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:

<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;
    }
}
+1
source

Source: https://habr.com/ru/post/1713768/


All Articles