【问题标题】:Binding data to the Listbox where ListBox has textblocks将数据绑定到 ListBox 具有文本块的 Listbox
【发布时间】:2011-11-21 16:35:17
【问题描述】:
<ListBox Height="498" Margin="2,0,0,0" Name="listBox1" Width="879"  ItemsSource="{Binding}" >
   <ListBoxItem >
       <StackPanel Width="418" Orientation="Horizontal">
           <TextBlock Name="MedicineName" Text="Alamoxy" 
                      FontWeight="Bold" FontSize="18" 
                      Margin="5" Width="205" >
            </TextBlock>
            <TextBlock Name="ListBoxLetter" Text="Amoksilin" 
                       FontSize="18" Margin="0" Width="255" Height="23">
            </TextBlock>
       </StackPanel>
    </ListBoxItem>
</ListBox>

我想将数据绑定到列表框。 文本块将显示单独的字段。我该怎么做? 请帮我解决这个问题/

【问题讨论】:

    标签: wpf xaml data-binding wpf-controls


    【解决方案1】:

    使用ItemTemplate 使其看起来像这样。

    <ListBox Width="400" Margin="10" ItemsSource="{Binding myItems}">
       <ListBox.ItemTemplate>
         <DataTemplate>
           <StackPanel>
             <TextBlock Text="{Binding Path=MedicineName}" />
             <TextBlock Text="{Binding Path=ListBoxLetter}"/>
           </StackPanel>
         </DataTemplate>
       </ListBox.ItemTemplate>
     </ListBox>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-28
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      相关资源
      最近更新 更多