我有一个wpf列表框和两个按钮(向上和向下)。
如果用户选择列表中的任何项目并单击向下按钮,则以下所有项目(包括所选项目)必须向下移动。同样,点击向上按钮,它应该向上移动。
示例说明如下: -
Student 1 Student 2 Student 3
Sem 1 1 1
Physics 68 87 70
Chemistry 78 89 78
Math 62 77 80
Sem 2 2 2
Physics 78 69 78
Chemistry 58 79 88
Math 72 67 90
在上表中,如果用户为学生2选择第一个学期项目并单击向下按钮,则学生2的数据应向下移动,如下所示: -
Student 1 Student 2 Student 3
Sem 1 1
Physics 68 70
Chemistry 78 78
Math 62 80
Sem 2 1 2
Physics 78 87 78
Chemistry 58 89 88
Math 72 77 90
转移的数据应该是可检索的。 我正在使用observable collection来绑定listbox的itemsource。 以下是我的示例代码: -
<Window.Resources>
<DataTemplate x:Key="myTemplate">
<StackPanel>
<Label Background="Purple" Foreground="White" BorderBrush="Red" BorderThickness="4">
<Label.Content>
<WrapPanel HorizontalAlignment="Stretch">
<TextBlock>Student Name:</TextBlock>
<TextBlock Text="{Binding Name}" />
</WrapPanel>
</Label.Content>
</Label>
<WrapPanel>
<ListBox ItemsSource="{Binding LstSubjects}" BorderThickness="0" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<WrapPanel Grid.Row="0">
<TextBlock> Sem:</TextBlock>
<TextBlock Text="{Binding Semester}"></TextBlock>
</WrapPanel>
<WrapPanel Grid.Row="1">
<TextBlock> Physics:</TextBlock>
<TextBlock Text="{Binding Physics}"></TextBlock>
</WrapPanel>
<WrapPanel Grid.Row="2">
<TextBlock> Chemistry:</TextBlock>
<TextBlock Text="{Binding Chemistry}"></TextBlock>
</WrapPanel>
<WrapPanel Grid.Row="3">
<TextBlock> Maths:</TextBlock>
<TextBlock Text="{Binding Maths}"></TextBlock>
</WrapPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</WrapPanel>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding StudentModel}" ItemTemplate="{StaticResource myTemplate}" Margin="0,0,0,0" VerticalAlignment="Top">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
请注意,列表框包含学生姓名和所有科目标记列表(Sem,物理,化学,数学)。
提前致谢。
答案 0 :(得分:0)
以下是一个示例,为您提供了两种方法,展示了在wpf中处理数据的常用方法。这不是您原始问题的答案。 从您的评论中,您似乎想要转向另一个方向,所以我将其作为答案发布。可以随意重新定义你的意图,我将编辑这篇文章。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MyViewmodel/>
</Window.DataContext>
<StackPanel>
<!-- let Datagrid create the columns -->
<DataGrid Name="MyDataGrid" ItemsSource="{Binding Students}">
</DataGrid>
<!-- A listview with itemtemplate grouping the proprties -->
<ListView ItemsSource="{Binding Students}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"></TextBlock>
<TextBlock Text="{Binding Semester}"></TextBlock>
<TextBlock Text="{Binding Physics}"></TextBlock>
<TextBlock Text="{Binding Chemistry}"></TextBlock>
<TextBlock Text="{Binding Math}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
..和cs代码:
namespace WpfApplication1
{
public class Student
{
public string Name { get; set; }
public int Semester { get; set; }
public int Physics { get; set; }
public int Chemistry { get; set; }
public int Math { get; set; }
};
public class MyViewmodel
{
public MyViewmodel()
{
Students = new ObservableCollection<Student>
{
new Student { Name="John", Semester = 1, Physics = 50, Chemistry = 60, Math = 70 },
new Student { Name="Tim", Semester = 2, Physics = 80, Chemistry = 50, Math = 70 },
new Student { Name="Aaron", Semester = 3, Physics = 40, Chemistry = 90, Math = 70 }
};
}
public ObservableCollection<Student> Students { get; set; }
};
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}