2009-07-28 7 views
3

私はいくつかのデータをWPFリストビューにバインドしようとしています。私のデータ型のプロパティの1つはbyte[]なので、カンマ区切りの文字列として表示したいので、例えば{ 12, 54 }Byte[] Arrayではなく12, 54と表示されます。私はカスタムを作りたいと思うと思います。DataTemplate私は確信していません。それが最善の方法ですか?もしそうなら、どうすればいいですか?そうでない場合、最良の方法は何ですか?バイト配列をカンマ区切りの文字列としてフォーマットするには、WPFリストビューを取得するにはどうすればよいですか?

編集:これは1つの列に対してのみ使用したい - 他のプロパティはそのまま表示されます。

答えて

11

私はValueConverter使用することをお勧めします:あなたのXAMLで

[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 
} 

を、あなたの結合のようにそれを追加したいですこれは:

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

... 

"{Binding ByteArrayProperty, Converter={StaticResource byteArrayConverter}}" 
+1

方法について: '返すBitConverter.ToString((byte [])v alue).Replace( " - "、 "、"); 'あなたの' Convert'メソッドの本体として? – LukeH

+0

ああ、それに私を打つ。いい答え。 :) – Charlie

+0

@ルーク:それはいい考えです。私はBitConverterを一度も使用していませんが、それは便利です:) –

2

コンマで区切られた文字列を作成するValueConverterを作成します。次に、byte[]に直接バインドできますが、バインディングにコンバータを指定します。

(Markの答えは良いときに私は、コードを入れてするの必要はありません。)

1

次の例は、値コンバータを使用したバインディングを示しています。ここで

は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; } 
    } 
} 

は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> 

そしてここでは、値コンバータです:

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; 
    } 
} 
関連する問題