显示成员组合框,其中包含列表<Object>作为项目源



我有Comobox喜欢这个

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

在 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);

ArticlesStock类中,我有一个字符串Producer道具名称

如何在组合框中将其设置为我的路径?通常我们可以用道具来设置它。但在这里我有一个列表,里面还有一个列表。在此列表中,必须设置第一个项目值。C# 转换如何将其设置为显示成员

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

我会执行以下操作:为组合框的项目定义一个DataTemplate,并使用转换器来检索所需的属性。

数据模板定义:

<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>

以及用于访问 Producer 属性的转换器:

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;
    }
}

请注意,为了简洁起见,我简化了模型结构。

相关内容

最新更新