The easiest way I think to do this would be to add a property in your class which would be the int
value of the List
.
e.g.
int val { get { return ByteCount[0]; } }
Or you could also create a converter which would take the list as binding and the index as parameter, and return the desired value.
For example (I haven't tried this):
public class ElementOfListConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is IList && parameter is int)
{
var lst = value as IList;
int pos = (int)parameter;
if (lst.Count >= pos)
{
return lst[pos];
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Edit
These are the steps to use the converter:
- Create the class (ElementOfListConverter), as shown before.
- Add a new xml namespace pointing towards the location of your converter. E.g.
xmlns:local="clr-namespace:WpfApplication2"
In the resources of your Window (or UserControl), add a reference to your converter, like this:
<Window.Resources> <local:ElementOfListConverter x:Key="ElemOfList" /> </Window.Resources>
In your binding, specify the converter to use (using its key)
{Binding YourElement, Converter={StaticResource ElemOfList}, ConverterParameter=0}
The pro for this method is that you can specify the index of the element directly in the xaml. You could also bind this value to some other value using a MultiBinding
and a MultiValueConverter
. (see this question on S.O. if you need more information.