【发布时间】:2016-08-16 13:21:05
【问题描述】:
那么,如果我有一个变量集合,这些变量已经分配了数据并绑定到列表框项模板,我该如何根据列表框项的选择来获取数据集合?
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Height="500">
<Image Source="{Binding img }" Width="853" Height="480" VerticalAlignment="Top" Margin="0,10,8,0"/>
<StackPanel Width="370">
<TextBlock Text="{Binding text}" Foreground="#FFC8AB14" FontSize="28" />
<TextBlock Text="{Binding text2}" TextWrapping="Wrap" FontSize="24" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
C#:
public class collection
{
public string text { get; set; }
public string img { get; set; }
public string text2 { get; set; }
}
private void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox1.SelectedItems
//how do you get the selected data in the img, text, text2 on selection?
}
【问题讨论】:
-
为什么要绑定变量集合,而不是将这些变量作为属性分组到一个类中,然后绑定该类对象集合会更容易。
-
你有一个你也绑定的集合列表作为列表框的itemsource吗?还是您绑定到 1 个集合实例?
-
集合类只是模型,你如何填充你的列表框?