我有一个问题,我有一个datatable data,它有3个列( ID、NAME、QUANTITY),我想将它绑定到一个ListBox,并使ListBox显示来自列名和数量的值,否则当我双击所选的项时,它将发送ID值,这是我的XAML:
<ListBox HorizontalAlignment="Left" Margin="6,0,0,0" Name="ListBox1" VerticalAlignment="Top" Height="600" Width="321">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="MouseDoubleClick" Handler="ListBox1Item_DoubleClick" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.Resources>
<DataTemplate x:Key="listBoxTemplate">
<DockPanel >
<TextBlock FontWeight="Bold" Text="{Binding NAME}"
DockPanel.Dock="Left"
Margin="5,0,10,0" Width="100"/>
<TextBlock Text="{Binding QUANTITY} " Foreground="Green" FontWeight="Bold" />
</DockPanel>
</DataTemplate>
</ListBox.Resources>
</ListBox> 下面是我的代码:
...
ListBox1.ItemsSource = data.DefaultView;
ListBox1.SelectedValuePath = "ID";
...但它没有显示任何东西,什么不对吗?请帮帮忙!谢谢你看这个!
发布于 2012-06-19 14:06:37
您需要设置ListBox.ItemTemplate。目前,您正在用键定义模板,该模板在任何地方都不会使用。
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel >
<TextBlock Text="{Binding NAME}" FontWeight="Bold"
DockPanel.Dock="Left"
Margin="5,0,10,0" Width="100" />
<TextBlock Text="{Binding QUANTITY}" FontWeight="Bold"
Foreground="Green" />
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>https://stackoverflow.com/questions/11102634
复制相似问题