Reputation: 4417
I have a DataGrid that one of his columns supposed get the serial number of the row in the Grid, the DataGrid is binding to the following list:
public IList<xx> ListXX= new List<xx>();
xx class contains several variables that binding to the rest of the columns.
How can I bind the number of the column in the list to my column in DataGrid?
Upvotes: 3
Views: 858
Reputation: 5420
if you know your ListXX
Item order than you can do something like:
<DataGrid>
<DataGridTextColumn Header="#" IsReadOnly="True" Binding="{Binding LisXX[0]}"></DataGridTextColumn>
<DataGridTextColumn Header="#" IsReadOnly="True" Binding="{Binding LisXX[5]}"></DataGridTextColumn>
<DataGridTextColumn Header="#" IsReadOnly="True" Binding="{Binding LisXX[1]}"></DataGridTextColumn>
</DataGrid>
Upvotes: 1
Reputation: 4417
I solved the problem this way:
I created a converter to index list:
public class IndexConverter : IMultiValueConverter
{
public object Convert(
object[] values, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
var xx= values[0] as xx;
var listxx= values[1] as List<xx>;
if (xx== null)
return null;
return (listxx.FindIndex(x => x == xx) + 1).ToString();
}
public object[] ConvertBack(
object value, Type[] targetTypes,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
In DataGrid I bind the column with MultiBinding:
<DataGridTextColumn Header="#" IsReadOnly="True">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource indexConverter}">
<Binding />
<Binding RelativeSource=
"{RelativeSource AncestorType={x:Type DataGrid}}"
Path="ItemsSource"></Binding>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
Upvotes: 2
Reputation: 1525
With the limited information that you have provided . I am assuming that you are trying to bind the list to your WPF grid. You can use this as listed below.
this.dataGrid1.ItemsSource = list;
One more thing is make sure that in your XAML AutoGenerateColumn is set to true.
if this doesnt work. Post more information like how your XML looks like and how you are trying to bind it in code.
Upvotes: 1