Avinash Reddy
Avinash Reddy

Reputation: 937

DisplayMember ComboBox For Which Contains List<Object> as item source

I have Comobox likes this

 <dxe:ComboBoxEdit AutoComplete="True" IsTextEditable="False" ImmediatePopup="True" IncrementalFiltering="True" ItemsSource="{Binding Example}" />

In Vm

 public List<object> Example
            {
                get { return example; }
                set { example = value; OnPropertyChanged(new PropertyChangedEventArgs("Example")); }
            }
   public List<ArticlesStock> ArticlesStockList
        {
            get { return articlesStockList; }
            set
            {
                articlesStockList = value;
                OnPropertyChanged(new PropertyChangedEventArgs("ArticlesStockList"));
            }
        }

  Example.Add(ArticlesStockList);

In ArticlesStock class, I have Prop Name Producer a string

How can I set this as my path in ComboBox? normally we can just set this with props. but here I have a list .and inside I have one more list. in this list the first item value must be set. C# Conversion how can I set this as Display member

((List<ArticlesStock>)Example[0])[0].WarehouseDeliveryNoteItem.Producer;

Upvotes: 0

Views: 112

Answers (1)

Foglio
Foglio

Reputation: 101

I would do the following: define a DataTemplate for your combobox's items and use a converter to retrieve the property you need.

DataTemplate definition:

<ComboBox ItemsSource="{Binding Example}">
    <ComboBox.ItemTemplate>
        <DataTemplate DataType="{x:Type List}">
            <!--no Path is specified, which is equivalent to Path="."-->
            <TextBlock Text="{Binding Converter={StaticResource MyConv}}"></TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

And the converter used to access the Producer property:

public class MyConv : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // here value will be an item of Example list, so a List<ArticlesStock>
        var val = value as List<ArticlesStock>;
        return val[0].Producer;
    }
}

Note that I simplified your model structure for brevity.

Upvotes: 2

Related Questions