1

List 型のプロパティを XAML を介して ListBox にバインドしようとすると、うまくいきません。ただし、リストには文字列配列 (string[]) が含まれていることに注意してください。

したがって、コードの XAML 部分は次のようになります。

<ListBox Height="373" HorizontalAlignment="Left" Margin="9,65,0,0" Name="reservoirList" VerticalAlignment="Top" Width="546" 
                 ItemsSource="{Binding Wells}">
</ListBox>

およびviewModelで:

public ObservableCollection<string[]> Wells {
            get { return new ObservableCollection<string[]>(getWellsWithCoords()); }            
        }

ここで getWellsWithCoords() は string[] のリストを作成します。

アプリケーションを実行すると、次のように表示されます。これは理にかなっています。

string[] array
string[] array 
....

各行で自動的に Wells リストの各要素の n 値が表示されるように、XAML コードを変更することは可能ですか。つまり、次のようなものです。

well1 value11 value12
well2 value21 value22
4

2 に答える 2

3

リストボックスにテンプレートを追加します。

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

于 2012-09-13T14:07:01.620 に答える
0

このデータを保持するための独自のクラスを実装できます。

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

および XAML では:

<ListBox 
    ItemsSource="{Binding Wells}" 
    DisplayMemberPath="DisplayValue" />
于 2012-09-13T14:09:28.890 に答える