2017-12-05 9 views
0

wpf 목록 상자와 두 개의 단추 (위아래)가 있습니다.가로 방향 목록 상자에서 데이터 이동

사용자가 목록에서 임의의 항목을 선택하고 아래로 버튼을 클릭하면 다음 항목 (선택한 항목 포함)이 모두 아래쪽으로 이동해야합니다. 그리고 마찬가지로, 위로 버튼을 클릭하면 위로 움직여야합니다.

예가 아래에 설명되어있다 : - 사용자가 학생 2 제 학기 항목을 선택하고, 다운 버튼을 클릭하면, 위의 표에

    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

것 같습니다. 이를 위해 itemtemplate을 만들고 각 행에 정렬 할 수 있습니다. 데이터를 위로 이동하는 것은 매우 드문 일입니다. 기본적으로 나는 당신의 테이블 배열을 개선 할 수 있다고 생각합니다. – deafjeff

+0

@deafjeff : 답변 해 주셔서 감사합니다. 네 말이 맞아, 나는 각 학생을 위해 여러 가지 상품 묶음을 관리해야 해. 내가이 유스 케이스에 정말로 걸려 있기 때문에 약간의 sapmle 코드를 공유해 주시겠습니까? –

답변

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

+0

감사합니다. @deafjeff. 코드는 필요한 방식으로 데이터를 표시하는 문제를 해결합니다. DataGrid보다는 listview 또는 listbox를 사용하는 것이 더 좋습니다. Listbox를 사용하여 동일한 결과를 얻었습니다. 첫 번째 목록 상자의 itemtemplate에 각 학기의 점수를 그룹화 한 다른 목록 상자를 추가했습니다. 그러나 나는 여전히 데이터의 이동을 연구 중이다. –

+0

샘플 코드로 내 질문을 업데이트했습니다. –

+0

텍스트를 변경하지 않았지만 코드를 변경해도 항목이 세로로 이동하기를 원한다고 생각합니다. 다소 특이합니다. 내가 이해 한대로 : "아래로"를 클릭하면 아래에있는 행이 위에서 블록을 가져옵니다. 따라서 클릭 핸들러에서 원하는대로 ObservableCollection 내부의 데이터를 이동해야하며 업데이트해야합니다. 목록을 갱신 된 상태로 유지하려면 수정 된 항목을 완전히 대체하십시오. – deafjeff