2010-06-08 4 views
0

우리는WPF 목록보기 포토샵 층과 동일한 작동하는지 확인하는 방법

는 몇 가지 특징이있다, Photoshop 레이어 창 (또는 다른 이미지 editting에 도구)에서 볼 수 있듯이 :

  1. 그룹 폴더 등으로을 헤더 (접기/확장 가능)
  2. 펼쳐지지 않은 항목 (헤더 없음)
  3. "수동으로"드래그하여 정렬합니다.

1의 경우 확장기 컨트롤을 사용하여 groupped 항목을 만들었습니다. (ref)

아니요 3의 경우 수동으로 정렬을이 코드로 해결할 수 있습니다. (ref)

그러나 나는 잘 모르겠다. 목록에는 깨지지 않은 항목이 있습니다. 물론, 파손되지 않은 항목에는 헤더가 없을 수 있습니다.

ICollectionView 및 MVVM을 사용하고 있습니다.

ICollectionView로 만들 수 있습니까?

답변

2

ICollectionView 수 없습니다. GroupStyle.ContainerStyleSelector 수 있습니다. 다음은 그 예입니다.

XAML :

<Window x:Class="WpfWindowDrag.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:WpfWindowDrag="clr-namespace:WpfWindowDrag" 
     Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
    <!--Container style for a groupped item--> 
    <Style x:Key="RegularContainerStyle" TargetType="{x:Type GroupItem}"> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
      <Expander Header="{Binding Name}" IsExpanded="True"> 
       <ItemsPresenter /> 
      </Expander> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style> 

    <!--Container style for root objects (i.e. non grouped)--> 
    <Style x:Key="RootContainerStyle" TargetType="{x:Type GroupItem}"> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
      <ItemsPresenter /> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style> 

    <!--This guy lets us select choose proper group for an item.--> 
    <WpfWindowDrag:CountryStyleSelector x:Key="ContainerStyleSelector" 
             RegularGroupStyle="{StaticResource RegularContainerStyle}" 
             RootGroup="{StaticResource RootContainerStyle}" /> 
    </Window.Resources> 
    <Grid> 
    <ListView x:Name="lv"> 
     <ListView.GroupStyle> 
     <GroupStyle ContainerStyleSelector="{StaticResource ContainerStyleSelector}" /> 
     </ListView.GroupStyle> 
     <ListView.View> 
     <GridView> 
      <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" /> 
     </GridView> 
     </ListView.View> 
    </ListView> 
    </Grid> 
</Window> 

CS :

using System; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 

namespace WpfWindow 
{ 
    public partial class Window1 : Window 
    { 
    private const int NumberOfRecords = 20; 
    private readonly ObservableCollection<Person> _myList; 

    public Window1() 
    { 
     InitializeComponent(); 

     var countries = new[] { "", "US", "China", "India", "Japan", "Ukraine" }; 

     var countriesCount = countries.Length; 
     _myList = new ObservableCollection<Person>(); 
     var rnd = new Random(); 

     for (int i = 0; i < NumberOfRecords; i++) 
     { 
     int countryIndex = rnd.Next(countriesCount); 
     _myList.Add(new Person() { Name = string.Format("Name {0}", i), Country = countries[countryIndex] }); 
     } 

     ICollectionView view = CollectionViewSource.GetDefaultView(_myList); 
     view.GroupDescriptions.Add(new PropertyGroupDescription("Country")); 
     view.SortDescriptions.Add(new SortDescription("Country", ListSortDirection.Ascending)); 
     view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); 

     lv.ItemsSource = view; 
    } 
    } 

    public class Person 
    { 
    public string Name { get; set; } 
    public string Country { get; set; } 
    } 

    public class CountryStyleSelector : StyleSelector 
    { 
    public Style RegularGroupStyle { get; set; } 

    public Style RootGroup { get; set; } 

    public override Style SelectStyle(object item, DependencyObject container) 
    { 
     var cvg = item as CollectionViewGroup; 
     if (cvg == null) 
     { 
     return base.SelectStyle(item, container); 
     } 
     return string.IsNullOrEmpty(cvg.Name as string) ? RootGroup : RegularGroupStyle; 
    } 
    } 
} 

항목 그룹을 가진, 그러나 명심 자동으로 가상화 및 영향 성능을 사용할 수 없습니다. 만약 수천 가지 아이템을 가지고 정말 빠른 것을 필요로한다면 아마도 자신의 컨트롤을 작성하거나 제 3자를 찾아야 할 것입니다.

환호, Anvaka.

+0

Anvaka, 도움 주셔서 감사합니다. 성공적으로 그루핑 된 코드와 함께 코드화되지 않은 항목을 표시합니다. 그러나 내 문제는 groupped 항목은 항상 하단에 위치한다는 것입니다. 내 생각에, 그룹 자체에는 깨지지 않은 항목 사이에 위치시키기위한 정렬 설명이 없습니다. 그러나 목록보기에서 정리되지 않은 항목과 그루핑 된 항목을 어떻게 혼합 할 수 있습니까? – Youngjae

+0

Youngjae, 여기에 설명 된대로 두 가지 SortDescription을 사용할 수 있습니다. http://bea.stollnitz.com/blog/?p=17 – Anvaka

+0

Thans in advance. 나는 그것이 완전히 신선해질 필요가있을 것이라고 생각한다. 순진한 스택 패널을 사용하여 문제를 해결하려고 노력할 것입니다. – Youngjae

관련 문제