C# 将列表框数据绑定到列表<;字符串[]>;

C# 将列表框数据绑定到列表<;字符串[]>;,c#,wpf,xaml,data-binding,listbox,C#,Wpf,Xaml,Data Binding,Listbox,我很难通过XAML将我的List类型的属性绑定到ListBox。但请注意,该列表包含字符串数组(字符串[]) 因此,代码的XAML部分如下所示: <ListBox Height="373" HorizontalAlignment="Left" Margin="9,65,0,0" Name="reservoirList" VerticalAlignment="Top" Width="546" ItemsSource="{Binding Wells}">

我很难通过XAML将我的List类型的属性绑定到ListBox。但请注意,该列表包含字符串数组(字符串[])

因此,代码的XAML部分如下所示:

<ListBox Height="373" HorizontalAlignment="Left" Margin="9,65,0,0" Name="reservoirList" VerticalAlignment="Top" Width="546" 
                 ItemsSource="{Binding Wells}">
</ListBox>
是否可以以这样的方式更改XAML代码,以便在每行上自动 我看到Wells列表中每个元素的n值,例如:

well1 value11 value12
well2 value21 value22

将模板添加到列表框:

<ListBox Height="373" HorizontalAlignment="Left" Margin="9,65,0,0" Name="reservoirList" VerticalAlignment="Top" Width="546" ItemsSource="{Binding Wells}">
<ListBox.ItemTemplate>
    <DataTemplate>
        <!--Here you bind the array-->
        <ItemsControl ItemsSource="{Binding}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <!--Here you bind the value of each string-->
                <DataTemplate>
                    <TextBlock Text="{Binding}" Margin="5,0"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </DataTemplate>
</ListBox.ItemTemplate>


您可以实现自己的类来保存这些数据:

public class WellInfo
{
    private string[] _infos;
    public WellInfo(string[] infos)
    {
        this._infos = infos;
    }
    public string DisplayValue
    {
        get { return this._infos.Aggregate((current, next) => current + ", " + next); }
    }
}
在您的收藏中使用:

public IEnumerable<WellInfo> Wells
{
    get { return getWellsWithCoords().Select(x => new WellInfo(x)); }            
}
公共IEnumerable井
{
获取{return getWellsWithCoords()。选择(x=>newwellinfo(x));}
}
在XAML中:

<ListBox 
    ItemsSource="{Binding Wells}" 
    DisplayMemberPath="DisplayValue" />

public IEnumerable<WellInfo> Wells
{
    get { return getWellsWithCoords().Select(x => new WellInfo(x)); }            
}
<ListBox 
    ItemsSource="{Binding Wells}" 
    DisplayMemberPath="DisplayValue" />