Sim*_*mon 3 c# data-binding wpf listview datatemplate
我正在尝试将一些数据绑定到WPF列表视图.我的数据类型的一个属性是类型byte[],我希望它显示为逗号分隔的字符串,因此例如{ 12, 54 }将显示为12, 54而不是Byte[] Array.我想我想制定一个习惯,DataTemplate但我不确定.这是最好的方式吗?如果是这样,我该怎么办?如果没有,最好的方法是什么?
编辑:我只想将它用于一列 - 其他属性显示正常.
Mar*_*iec 11
我建议使用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
}
Run Code Online (Sandbox Code Playgroud)
在你的xaml中,你可以像这样将它添加到你的绑定中:
<Window.Resources>
<local:ByteArrayConverter x:Key="byteArrayConverter"/>
</Window.Resources>
...
"{Binding ByteArrayProperty, Converter={StaticResource byteArrayConverter}}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2774 次 |
| 最近记录: |