使用 MVVM 在 ListBox 中绑定

Binding in ListBox using MVVM

我在将所有数组条目绑定到 XAML 中的 ListBox 时遇到问题。

XAML:

<ListBox ItemsSource="{Binding ResultFlag}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding TypeInfo}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

ResultFlag 属性 在我的 ViewModel 中(这是 XAML 文件的 DataContext):

private ObservableCollection<DataField> _resultFlag;
public ObservableCollection<DataField> ResultFlag
{
    get { return _resultFlag; }
    set
    {
         _resultFlag = value;
         OnPropertyChanged();
    }
}

TypeInfoDataField class:

public string[] TypeInfo { get; set; }

我想在 ListBox 中显示上述数组中的所有字符串条目 - 我应该怎么做?我尝试了几种方法,包括嵌套 Listbox 和将 ListBoxItemsSource 直接绑定到数组(没用,顺便说一句)
干杯!

您的场景中有一个列表列表。为了在您的列表框中显示,您需要像这样嵌套列表框。

       <ListBox ItemsSource="{Binding ResultFlag}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding TypeInfo}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>