سؤال

My class is this :

public class Articolo : Screen
{
        public string Code { get; set; }
        public string Description { get; set; }
        public decimal Cost{ get; set; }
        public decimal Price{ get; set; }
        public List<Ean> BarCode { get; set; }
}

and this Ean Class:

public class Ean
{
    public string Code{ get; set; }
}

My datagrid is :

<DataGrid Height="367" HorizontalAlignment="Stretch" Margin="14,52,12,0"
         VerticalAlignment="Top" AutoGenerateColumns="False" x:Name="List" >
       <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Code}" Header="Codice" />
            <DataGridTextColumn Binding="{Binding Description }" Header="Descrizione" />
            <DataGridTextColumn Binding="{Binding Cost}" Header="Quantita" />
            <DataGridTextColumn Binding="{Binding Price}" Header="Prezzo" />
            <DataGridTextColumn Binding="{Binding BarCode}" Header="BarCodes" />
        </DataGrid.Columns>
</DataGrid>

He is a part of ViewModel:

    [Export(typeof(IScreen))]
    public class BolleViewModel : Screen
    {
        public List<Articolo> List { get; private set; }

        public BViewModel()
        {
            Recover recover = new Recover();
            List = recover.Import();
            NotifyOfPropertyChange("List");
        }
    }

In the columns BarCodes is write (Collection)! And not the barcodes. Why? How can I view the list of barcodes in the column? thanks..

هل كانت مفيدة؟

المحلول

BarCode is a List<T> and the ToString() method of List<T> does not by itself display the contents of the list.

The easiest thing you can do is probably to implement an IValueConverter and apply this converter in your BarCode binding.

As a start, the converter could look something like this:

public class EanListToStringConverter : IValueConverter {
   public object Convert(object value, Type targetType, 
                         object parameter, CultureInfo culture) {
      return String.Join(" ", ((List<Ean>)value).Select(ean => ean.Code));
   }
   public object ConvertBack(object value, Type targetType, 
                             object parameter, CultureInfo culture) {
      return NotSupportedException();
   }
}

Then, you need to add a converter instance as a resource in your view:

<Grid.Resources>
    <converters:EanListToStringConverter x:Key="EanListToString"/>
</Grid.Resources>

And finally in your BarCode binding invoke this instance, something like this:

<DataGridTextColumn Binding="{Binding BarCode, 
    Converter={StaticResource EanListToString}}" Header="BarCodes" />
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top