开发者

ListBox showing one property in different rows

开发者 https://www.devze.com 2023-02-25 12:54 出处:网络
I am using the following code for binding ListBox to a list i.e. List and set the binding Path=Name. But the list box shows just the one name with letter divided in rows. Like if a Name is JOHN, the l

I am using the following code for binding ListBox to a list i.e. List and set the binding Path=Name. But the list box shows just the one name with letter divided in rows. Like if a Name is JOHN, the list box row 1 shows "J", row 2 shows "O", row 3 shows "H", row 4 shows "N". Here's the code.

Xaml

<ListBox Height="Auto" ItemsSource="{Bind开发者_运维技巧ing Path=Name}" HorizontalAlignment="Stretch" Margin="0,80,0,0" Name="ledgerListView" VerticalAlignment="Stretch" Width="200" KeyDown="ledgerListView_KeyDown" MouseDoubleClick="ledgerListView_MouseDoubleClick" IsSynchronizedWithCurrentItem="True" />

Code-Behind

List<Ledgers> ledgers = new List<Ledgers>();
        ledgers = DAL_Ledgers.LoadLedgers();
        this.DataContext = ledgers;


The ItemsSource property needs to be bound to the source collection that you want to generate the list box's items from. In this case that would just be the DataContext. To show the name for each item you can either apply a DataTemplate to the ItemTemplate property containing what you want to show for each item, or for a simple case like this just use the DisplayMemberPath to specify the Name property.

<ListBox ItemsSource="{Binding}" DisplayMemberPath="Name" x:Name="ledgerListView"/>


It looks like you're binding to the wrong thing... Does it work if you use:

<ListBox ItemsSource="{Binding}" ...>
  <ListBox.ItemTemplate>
    <DataTemplate>
      <sdk:Label Content="{Binding Path=Name}" />
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>


Also, you might want to use an ObservableList, otherwise changes in ledgers won't be taken into account.

0

精彩评论

暂无评论...
验证码 换一张
取 消