Binding Array string to a textblock
-
Yes. Do you want the text to be just one item in the array, or all of them seperated by a comma or something?
-
Yes. Do you want the text to be just one item in the array, or all of them seperated by a comma or something?
-
For a single item you can specify the index in the binding (StrArray would be the string array in your data class):
Text="{Binding Path=StrArray[0]}"
The trick to get all the values in a string seperated by commas is to use a value converter - something like this:
public class StrArrayConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string[] strArr = value as string[];
if (strArr != null)
{
StringBuilder sb = new StringBuilder();
foreach (string s in strArr)
{
sb.Append(s);
sb.Append(", ");
}
return sb.ToString();
}return null; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; }
}
<TextBlock>
<TextBlock.Text>
<Binding Path="StrArr">
<Binding.Converter>
<l:StrArrayConverter/>
</Binding.Converter>
</Binding>
</TextBlock.Text>
</TextBlock>