2017-12-05 23 views
0

私はwpfリストボックスと2つのボタン(上下)を持っています。水平方向のリストボックスでのデータ移動

ユーザーがリスト内の任意の項目を選択して下ボタンをクリックすると、次の項目(選択項目を含む)はすべて下方向にシフトする必要があります。同様に、上のボタンをクリックすると上に移動します。

例が以下に説明されている: - ユーザが学生2ための第1学期の項目を選択してボタンダウンをクリックした場合、上記の表に

    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のデータは下に移動すべきです -

    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コレクションを使ってリストボックスの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

あなたが "SEM /物理学/化学/数学" の項目-バンドルを管理するように見えます。そのためのitemtemplateを作成し、それを各行に配置することができます。データを上に移動することは非常に珍しいことです。基本的には、あなたのテーブルの配置が改善されていると思います。 – deafjeff

+0

@deafjeff:あなたの答えをありがとう。あなたは正しいです、私は各学生のためにいくつかの商品バンドルを管理する必要があります。私は本当にこのユースケースに固執しているので、いくつかのsapmleコードを共有してください。 –

答えて

0

ここでは、wpfでデータを処理する一般的な方法を示す2つのアプローチを示すサンプルを示します。これはあなたの元の質問に対する答えではありません。 あなたのコメントから、あなたは別の方向に行きたいと思うので、回答として投稿します。あなたの意図を自由に変更してください。私はこの投稿を編集します。

<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(); 
    }  
} 
} 

+0

ありがとう@deafjeff。あなたのコードは、必要な方法でデータを表示するという問題を解決します。 DataGridではなくlistviewやlistboxを使うのが良い考えです。私はListboxを使って同じことを達成しました。各学期のマークをグループ化する最初のリストボックスのitemtemplateに別のリストボックスを追加しました。しかし、私はまだデータの移動に取り組んでいます。 –

+0

サンプルコードで質問を更新しました。 –

+0

テキストを変更するのではなくコードを変更したので、アイテムを垂直方向に移動したいと思っています。少し珍しいことです。私が理解したように、「下へ」をクリックすると、下にある行が上からブロックを取得します。したがって、クリックハンドラではObservableCollection内のデータを好きなだけシフトしてから更新する必要があります。リストを更新したままにするには、変更したアイテムを完全に置き換えます。 – deafjeff

関連する問題